链接数据库
作者:互联网
import pymysql
# 建立连接
conn = pymysql.connect(host='localhost', user='root', password='flexoffice', db='pc', charset='utf8')
# 创建链接的对象
cur = conn.cursor()
# sql语句
sql1 = 'insert into student (s_id,s_name,s_brith,s_sex) values (13,"张三","1991-01-21","男")'
sql = 'select * from student'
# 调用对象的execute使用sql语句
cur.execute(sql1)
# (('01', '语文', '02'), ('02', '数学', '01'), ('03', '英语', '03'))
cur.execute(sql)
conn.commit()
# 查询需要返回值用于查看。修改则无需
emps = cur.fetchall()
# 关闭数据库
conn.close()
print(emps)
标签:execute,01,cur,数据库,pymysql,sql,链接,conn 来源: https://www.cnblogs.com/tfxbk/p/16355807.html