python——连接数据库

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)
posted @ 2022-06-08 15:23  非同凡响  阅读(34)  评论(0编辑  收藏  举报