码迷,mamicode.com
首页 > 数据库 > 详细

Python 之 增删改查Mysql数据库

时间:2019-03-12 15:32:32      阅读:205      评论:0      收藏:0      [点我收藏+]

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

表结构:

技术图片

代码:

#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   打印   

原文地址:https://blog.51cto.com/12965094/2361750

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!