本文主要给大家介绍Python标准库MySQL工作流程是怎么样的,文章内容都是笔者用心摘选和编辑的,具有一定的针对性,对大家的参考意义还是比较大的,下面跟笔者一起了解下Python标准库MySQL工作流程是怎么样的 吧。
MySQLdb工作流程如下:

connection
connection方法用于创建客户端与数据库的网络连接.
语法:
MySQLdb.Connect(参数)
参数
参数 | 类型 | 说明 |
---|
host | 字符串 | MySQL云服务器地址 |
port | 整型 | MySQL云服务器端口号 |
user | 字符串 | MySQL数据库用户名 |
passwd | 字符串 | MySQL数据库密码 |
db | 字符串 | MySQL数据库库名 |
charset | 字符串 | 连接所使用的字符集 |
例如:
# 导入MySQLdb模块
>>> import MySQLdb
# 创建一个Connect连接
>>> conn = MySQLdb.Connect(host='127.0.0.1', user='root', passwd='as', db='USER', port=3306, charset="utf8")
>>> cursor = conn.cursor()
>>> print(cursor)
<MySQLdb.cursors.Cursor object at 0x7f4af5e15550>
>>> print(conn)
<_mysql.connection open to '127.0.0.1' at 15b1518>
# 关闭连接
>>> conn.close()
>>> print(conn)
<_mysql.connection closed at 15b1518>
connection对象支持的方法
方法名 | 说明 |
---|
cursor() | 使用该连接创建并返回游标 |
commit() | 提交当前事务 |
rollback() | 回滚当前事务 |
close() | 关闭连接 |
cursor
cursor用户执行查询和获取结果,执行流程如下:

cursor对象所支持的方法
参数名 | 说明 |
---|
execute(“SQL”) | 执行的SQL语句 |
fefchone() | 获取结果的下一行 |
fefchmany(size) | 获取结果的下几行 |
fefchall() | 获取结果剩下的所有行 |
rowcount | 最近一次execute返回数据的行数或影响的行数 |
close() | 关闭游标对象 |
事务
访问额更新数据库的一个程序执行单元,执行单元指的就是很多操作的集合,里面的每个操作都是用来访问个更新数据库.
比如银行转账,A用户向B用户转账100,A-100和B+100这两个操作,要么都做,要么都不操作
一致性: 事务必须使数据库从一致性状态变到另一个一致性状态
隔离性: 一个事务的执行不能被其他事务干扰
持久性: 事务一旦提交,他对数据库的改变是永久性的
开发中怎样使用事务?
关闭自动commit: 设置conn.autocommit(False),MySQLdb默认已经为False
正常结束事务: conn.commit()
异常结束事务: conn.rollback()
实例
先创建一个user表:
CREATE DATABASE USER;
USE USER;
CREATE TABLE `user` (
`userid` INT(11) NOT NULL AUTO_INCREMENT,
`username` VARCHAR(100) DEFAULT NULL,
PRIMARY KEY (`userid`)
) ENGINE=INNODB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;
插入以下内容
INSERT INTO user(userid, username) VALUES(1, 'name1');
INSERT INTO user(userid, username) VALUES(2, 'name2');
INSERT INTO user(userid, username) VALUES(3, 'name3');
INSERT INTO user(userid, username) VALUES(4, 'name4');
INSERT INTO user(userid, username) VALUES(5, 'name5');
查看数据
mysql> SELECT * FROM user;
+--------+----------+
| userid | username |
+--------+----------+
| 1 | name1 |
| 2 | name2 |
| 3 | name3 |
| 4 | name4 |
| 5 | name5 |
+--------+----------+
5 rows in set (0.00 sec)
>>> import MySQLdb
>>> conn = MySQLdb.Connect(host='127.0.0.1', user='root', passwd='as', db='USER', port=3306, charset="utf8")
>>> cursor = conn.cursor()
>>> SQL = "SELECT * FROM user"
# 返回获取到的多少行
>>> cursor.execute(SQL)
5
# 输出获取到的行数
>>> print(cursor.rowcount)
5
# 返回第一条数据
>>> cursor.fetchone()
(1, 'name1')
# 返回两条数据
>>> cursor.fetchmany(2)
((2, 'name2'), (3, 'name3'))
# 返回剩下的所有数据
>>> cursor.fetchall()
((4, 'name4'), (5, 'name5'))
流程图:

>>> import MySQLdb
>>> conn = MySQLdb.Connect(host='127.0.0.1', user='root', passwd='as', db='USER', port=3306, charset="utf8")
>>> cursor = conn.cursor()
>>> cursor.execute("INSERT INTO user(userid, username) VALUES(50, 'name50')")
1
>>> cursor.execute("UPDATE user SET username='as' WHERE userid=1")
1
>>> cursor.execute("DELETE FROM user WHERE userid=2")
1
>>> conn.commit()
>>> cursor.close()
>>> conn.close()
查看数据库表内容
mysql> SELECT * FROM user;
+--------+----------+
| userid | username |
+--------+----------+
| 1 | as |
| 3 | name3 |
| 4 | name4 |
| 5 | name5 |
| 50 | name50 |
+--------+----------+
5 rows in set (0.00 sec)
#Python标准库 #Mysqldb
看完以上关于Python标准库MySQL工作流程是怎么样的,很多读者朋友肯定多少有一定的了解,如需获取更多的行业知识信息 ,可以持续关注我们的行业资讯栏目的。