pymysql增删改查
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # import pymysql 2 # 3 # # 建立连接 4 # conn = pymysql.connect(host='192.168.31.27', port=3306, user='root', password='123', database='db2', charset='utf8') 5 # # 游标 6 # cursor = conn.cursor() # 执行完毕返回的结果集默认以元组显示 7 # # 执行sql 8 9 10 # 增删改查 11 12 sql = 'insert into userinfo(user,pwd) values(%s,%s)' 13 # rows = cursor.execute(sql, ('wxx', '123')) 14 # # print(rows) 15 # res=cursor.executemany(sql,[("root","123456"),("lhf","12356"),("eee","156")]) 16 # 17 # conn.commit() 18 19 # res=cursor.execute(sql) #执行sql语句,返回sql影响成功的行数 20 # print(res) 21 22 # 关闭 23 # cursor.close() 24 # conn.close() 25 26 27 # import pymysql 28 # #链接 29 # conn = pymysql.connect(host='192.168.31.27', port=3306, user='root', password='123', database='db2', charset='utf8') 30 # #游标 31 # cursor=conn.cursor() 32 # 33 # #执行sql语句 34 # sql='select * from userinfo;' 35 # rows=cursor.execute(sql) #执行sql语句,返回sql影响成功的行数rows,将结果放入一个集合,等待被查询 36 # 37 # # cursor.scroll(3,mode='absolute') # 相对绝对位置移动 38 # # cursor.scroll(3,mode='relative') # 相对当前位置移动 39 # res1=cursor.fetchone() 40 # res2=cursor.fetchone() 41 # res3=cursor.fetchone() 42 # res4=cursor.fetchmany(2) 43 # res5=cursor.fetchall() 44 # print(res1) 45 # print(res2) 46 # print(res3) 47 # print(res4) 48 # print(res5) 49 # print('%s rows in set (0.00 sec)' %rows) 50 # 51 # 52 # 53 # conn.commit() #提交后才发现表中插入记录成功 54 # cursor.close() 55 # conn.close() 56 # 57 # ''' 58 # (1, 'root', '123456') 59 # (2, 'root', '123456') 60 # (3, 'root', '123456') 61 # ((4, 'root', '123456'), (5, 'root', '123456')) 62 # ((6, 'root', '123456'), (7, 'lhf', '12356'), (8, 'eee', '156')) 63 # rows in set (0.00 sec) 64 # ''' 65 66 67 import pymysql 68 69 conn = pymysql.connect(host='192.168.31.27', port=3306, user='root', password='123', database='db2', charset='utf8') 70 cursor = conn.cursor() 71 72 sql = 'insert into userinfo(user,pwd) values("wxx","123");' 73 rows=cursor.execute(sql) 74 print(cursor.lastrowid) #在插入语句后查看 75 76 conn.commit() 77 78 cursor.close() 79 conn.close()