下文给大家带来关于使用pymysql实现增删改查Mysql数据库,感兴趣的话就一起来看看这篇文章吧,相信看完使用pymysql实现增删改查Mysql数据库对大家多少有点帮助吧。
#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)
#执行插入单条带变量的语句
#data = cursor.execute("insert into user(username,age,department) values('{0}','{1}','{2}')".format(username,age,department))
#提交语句
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()看了以上关于使用pymysql实现增删改查Mysql数据库详细内容,是否有所收获。如果想要了解更多相关,可以继续关注我们的行业资讯板块。
亿速云「云数据库 MySQL」免部署即开即用,比自行安装部署数据库高出1倍以上的性能,双节点冗余防止单节点故障,数据自动定期备份随时恢复。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。