当前位置:Gxlcms > 数据库问题 > Python 之 增删改查Mysql数据库

Python 之 增删改查Mysql数据库

时间:2021-07-01 10:21:17 帮助过:14人阅读

表结构:

技术图片

代码:

#Author Kang

import pymysql

#连接方法
conn = pymysql.connect(host=‘10.3.152.35‘,port=3306,user=‘kang‘,passwd=‘123456‘,db=‘test‘)

#创建当前游标
cursor = conn.cursor()

#effect_row 为结果行数
effect_row = cursor.execute("select * from student")

#打印当前游标语句查询的结果
print(cursor.fetchall())

#建立需要插入的数据
data = [
    (‘MM‘,33),
    (‘BearBear‘,4),
    (‘KK‘,10)
]

#执行插入语句
cursor.executemany(‘insert into student(name,age) values(%s,%s)‘,data)

#提交语句
conn.commit()

#执行更新语句
cursor.execute(‘update student set name="MK" where id = 1‘)

conn.commit()

#执行删除语句
cursor.execute(‘delete from student where name = "KK"‘)

conn.commit()

cursor.close()

conn.close()

Python 之 增删改查Mysql数据库

标签:ESS   ext   water   imp   delete   commit   查询   fetch   打印   

人气教程排行