您好,欢迎访问一九零五行业门户网

Python 3.x 连接数据库示例(pymysql 方式)

由于 mysqldb 模块还不支持 python3.x,所以 python3.x 如果想连接mysql需要安装 pymysql 模块。
pymysql 模块可以通过 pip 安装。但如果你使用的是 pycharm ide,则可以使用 project python 安装第三方模块。
[file] >> [settings] >> [project: python] >> [project interpreter] >> [install按钮]
由于python统一了数据库连接的接口,所以 pymysql 和 mysqldb 在使用方式上是类似的:
pymysql.connect()参数说明
host(str):      mysql服务器地址
port(int):      mysql服务器端口号
user(str):      用户名
passwd(str):    密码
db(str):        数据库名称
charset(str):   连接编码
connection对象支持的方法
cursor()        使用该连接创建并返回游标
commit()        提交当前事务
rollback()      回滚当前事务
close()         关闭连接
cursor对象支持的方法
execute(op)     执行一个数据库的查询命令
fetchone()      取得结果集的下一行
fetchmany(size) 获取结果集的下几行
fetchall()      获取结果集中的所有行
rowcount()      返回数据条数或影响行数
close()         关闭游标对象
==================mysql===================
首先在连接数据库之前,先创建一个交易表,方便测试 pymysql 的功能:
drop table if exists `trade`; create table `trade` ( `id` int(4) unsigned not null auto_increment, `name` varchar(6) not null comment '用户真实姓名', `account` varchar(11) not null comment '银行储蓄账号', `saving` decimal(8,2) unsigned not null default '0.00' comment '账户储蓄金额', `expend` decimal(8,2) unsigned not null default '0.00' comment '账户支出总计', `income` decimal(8,2) unsigned not null default '0.00' comment '账户收入总计', primary key (`id`), unique key `name_unique` (`name`) ) engine=innodb auto_increment=2 default charset=utf8; insert into `trade` values (1,'乔布斯','18012345678',0.00,0.00,0.00);
==================python===================
使用python脚本实现增删改查和事务处理,源码如下:
import pymysql.cursors # 连接数据库 connect = pymysql.connect( host='localhost', port=3310, user='woider', passwd='3243', db='python', charset='utf8' ) # 获取游标 cursor = connect.cursor() # 插入数据 sql = "insert into trade (name, account, saving) values ( '%s', '%s', %.2f )" data = ('雷军', '13512345678', 10000) cursor.execute(sql % data) connect.commit() print('成功插入', cursor.rowcount, '条数据') # 修改数据 sql = "update trade set saving = %.2f where account = '%s' " data = (8888, '13512345678') cursor.execute(sql % data) connect.commit() print('成功修改', cursor.rowcount, '条数据') # 查询数据 sql = "select name,saving from trade where account = '%s' " data = ('13512345678',) cursor.execute(sql % data) for row in cursor.fetchall(): print("name:%s\tsaving:%.2f" % row) print('共查找出', cursor.rowcount, '条数据') # 删除数据 sql = "delete from trade where account = '%s' limit %d" data = ('13512345678', 1) cursor.execute(sql % data) connect.commit() print('成功删除', cursor.rowcount, '条数据') # 事务处理 sql_1 = "update trade set saving = saving + 1000 where account = '18012345678' " sql_2 = "update trade set expend = expend + 1000 where account = '18012345678' " sql_3 = "update trade set income = income + 2000 where account = '18012345678' " try: cursor.execute(sql_1) # 储蓄增加1000 cursor.execute(sql_2) # 支出增加1000 cursor.execute(sql_3) # 收入增加2000 except exception as e: connect.rollback() # 事务回滚 print('事务处理失败', e) else: connect.commit() # 事务提交 print('事务处理成功', cursor.rowcount) # 关闭连接 cursor.close() connect.close()
==================测试结果===================
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
更多python 3.x 连接数据库示例(pymysql 方式)。
其它类似信息

推荐信息