Python操作MySQL:pymysql模块
时间:2021-07-01 10:21:17
帮助过:37人阅读
pymysql
# 创建连接
conn = pymysql.connect(host=
‘127.0.0.1‘, port=3306, user=
‘root‘, password=
‘mysql8‘, db=
‘mysqltest‘)
# 创建游标(光标)
cursor =
conn.cursor()
# 执行SQL,返回影响行数
effect_rows = cursor.execute(
"select * from tt")
print(effect_rows)
# 取前两条
print(cursor.fetchmany(2
))
# 取第一条
print(cursor.fetchone())
# 取所有
print(cursor.fetchall())
# 用批量插入语句插入data
data = [(7,
‘gg‘),(8,
‘hh‘),(9,
‘ii‘)]
cursor.executemany("insert into tt values(%s, %s)", data)
conn.commit()
cursor.close()
conn.close()
Python操作MySQL:pymysql模块
标签:insert fetchall 连接mysql import mysqldb pass mysql any .com