import pymysql conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='work') cursor = conn.cursor () # create a cursor # Add single # r = cursor.execute("insert into class(caption)values (%s)",('BTCE1班',)) # Print (r) # print the number of affected # Print (cursor.lastrowid) # print increment id # --- batch increase # l = [("BTEC1班"),("BTEC2班"),("BTEC3班"),("BTEC4班"),] # r = cursor.executemany("insert into class(caption)values (%s)",l) # Print (r) # print the number of affected # new_id = cursor.lastrowid # Print (new_id) # print increment id # over_id = new_id +r # k = []; # # # Batch obtained from increasing the id cid; # while over_id > new_id: # k.append(new_id) # new_id += 1; # print(k) # conn.commit() # # Change # # Syntax updata column set on the data sheet provided ==% s where id. # r = cursor.execute("UPDATE class set caption = %s where cid = 44",('王玉',)) # conn.commit() # Print (r) # print the number of affected # Print (cursor.lastrowid) # print increment id # ----------- additions and deletions, require conn.commit () assigned from people #check # r = cursor.execute("select caption from class where cid = 44 or cid = 41 or cid = 40") # # The default sort order of sql, not sorted by sql query # print(cursor.fetchone()) # # Cursor.scroll (-1, mode = 'relative') # relative to the current position of the mobile # # Cursor.scroll (2, mode = 'absolute') # absolute relative position # Default tuple type cursor = conn.cursor (cursor = pymysql.cursors.DictCursor) # set to dictionary cursor.execute("select caption from class where cid = 44 or cid = 41 or cid = 40") print(cursor.fetchall()) # Again take over, the rest did not print(cursor.fetchall()) cursor.close() conn.close()
basis