使用pymysql实现增删改查Mysql数据库

发布时间:2020-06-04 14:28:20 作者:三月
来源:网络 阅读:246

下文给大家带来关于使用pymysql实现增删改查Mysql数据库,感兴趣的话就一起来看看这篇文章吧,相信看完使用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数据库详细内容,是否有所收获。如果想要了解更多相关,可以继续关注我们的行业资讯板块。

推荐阅读:
  1. 使用pyMySQL如何成功连接MySQL数据库
  2. 使用pymysql的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

pymysql mysql数据库

上一篇:YAML的常见语法格式

下一篇:图库中选择图片出现异常Cursor为null

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》