时间:2021-07-01 10:21:17 帮助过:52人阅读
import pymysql #链接 conn=pymysql.connect(host=‘localhost‘,user=‘root‘,password=‘123‘,database=‘egon‘) #游标 cursor=conn.cursor() #执行sql语句 #part1 # sql=‘insert into userinfo(name,password) values("root","123456");‘ # res=cursor.execute(sql) #执行sql语句,返回sql影响成功的行数 # print(res) #part2 # sql=‘insert into userinfo(name,password) values(%s,%s);‘ # res=cursor.execute(sql,("root","123456")) #执行sql语句,返回sql影响成功的行数 # print(res) #part3 sql=‘insert into userinfo(name,password) values(%s,%s);‘ res=cursor.executemany(sql,[("root","123456"),("lhf","12356"),("eee","156")]) #执行sql语句,返回sql影响成功的行数 print(res) conn.commit() #提交后才发现表中插入记录成功 cursor.close() conn.close()
import pymysql #链接 conn=pymysql.connect(host=‘localhost‘,user=‘root‘,password=‘123‘,database=‘egon‘) #游标 cursor=conn.cursor() #执行sql语句 sql=‘select * from userinfo;‘ rows=cursor.execute(sql) #执行sql语句,返回sql影响成功的行数rows,将结果放入一个集合,等待被查询 # cursor.scroll(3,mode=‘absolute‘) # 相对绝对位置移动 # cursor.scroll(3,mode=‘relative‘) # 相对当前位置移动 res1=cursor.fetchone() res2=cursor.fetchone() res3=cursor.fetchone() res4=cursor.fetchmany(2) res5=cursor.fetchall() print(res1) print(res2) print(res3) print(res4) print(res5) print(‘%s rows in set (0.00 sec)‘ %rows) conn.commit() #提交后才发现表中插入记录成功 cursor.close() conn.close() ‘‘‘ (1, ‘root‘, ‘123456‘) (2, ‘root‘, ‘123456‘) (3, ‘root‘, ‘123456‘) ((4, ‘root‘, ‘123456‘), (5, ‘root‘, ‘123456‘)) ((6, ‘root‘, ‘123456‘), (7, ‘lhf‘, ‘12356‘), (8, ‘eee‘, ‘156‘)) rows in set (0.00 sec) ‘‘‘
import pymysql conn=pymysql.connect(host=‘localhost‘,user=‘root‘,password=‘123‘,database=‘egon‘) cursor=conn.cursor() sql=‘insert into userinfo(name,password) values("xxx","123");‘ rows=cursor.execute(sql) print(cursor.lastrowid) #在插入语句后查看 conn.commit() cursor.close() conn.close()
mysql之pymysql模块
标签:value str dict ctc .com %s 用户名 comm cut