python连接mysql
时间:2021-07-01 10:21:17
帮助过:2人阅读
pymysql
import random
conn = pymysql.connect(host =
‘127.0.0.1‘,user =
‘lick‘,passwd=
‘123456‘,db=
‘pythontest‘)#建立连接
print(conn)
for i
in range(1,10000
):
idm = random.randint(1000,10000
)
name =
"test"+
str(idm)
cur =
conn.cursor()#建立游标,以游标为基本执行sql语句
cur.execute("show tables")#执行sql语句
sql =
"insert into tb01 values(%d,‘%s‘);"%
(idm,name)
cur.execute(sql)
cur.execute("select * from tb01")
conn.commit()#提交sql语句到database
ret =
cur.fetchall()#获取语句执行的信息
print(ret)
for x
in ret:
print(x)
cur.close()
conn.close()
python连接mysql
标签: