本地搭建的mysql的新建的表的数据如下:
在pycham中连接mysql 执行sql ,举例编写如下:
import pymysql
if __name__ == ‘__main__‘:
connect = pymysql.connect(host=‘localhost‘,
port=3307,
user=‘root‘,
password=‘111111‘,
database=‘autotest‘,
charset=‘utf8‘)
# 创建游标对象
cur = connect.cursor()
# 执行sql命令,注意防止sql注入
cur.execute(‘insert into autotest.stu_infos (stu_id,stu_name,stu_age,stu_grade) values ("NO_0005","小宁",29,89.88)‘) #执行insert语句
cur.execute("update %s set stu_name=‘%s‘ where stu_name=‘%s‘;" % ("autotest.stu_infos", "黄征", "小远")) #执行更新语句
cur.execute("DELETE FROM %s where stu_name=‘%s‘;" % ("autotest.stu_infos", "小杨")) #执行删除语句
cur.execute("select * from %s ;" % "autotest.stu_infos") #执行查询语句
value = cur.fetchall() #获取所有行
print(value)
connect.commit() # 提交数据修改
connect.close() # 关闭数据库链接