Python接入MySQL数据库
在 Python3 中,我们可以使用mysqlclient或者pymysql三方库来接入 MySQL 数据库并实现数据持久化操作。二者的用法完全相同,只是导入的模块名不一样。我们推荐大家使用纯 Python 的三方库pymysql,因为它更容易安装成功。下面我们仍然以之前创建的名为hrs的数据库为例,为大家演示如何通过 Python 程序操作 MySQL 数据库实现数据持久化操作。
接入MySQL
首先,我们可以在命令行或者 PyCharm 的终端中通过下面的命令安装pymysql,如果需要接入 MySQL 8,还需要安装一个名为cryptography的三方库来支持 MySQL 8 的密码认证方式。
1
| pip install pymysql cryptography
|
使用pymysql操作 MySQL 的步骤如下所示:
- 创建连接。MySQL 服务器启动后,提供了基于 TCP (传输控制协议)的网络服务。我们可以通过
pymysql模块的connect函数连接 MySQL 服务器。在调用connect函数时,需要指定主机(host)、端口(port)、用户名(user)、口令(password)、数据库(database)、字符集(charset)等参数,该函数会返回一个Connection对象。
- 获取游标。连接 MySQL 服务器成功后,接下来要做的就是向数据库服务器发送 SQL 语句,MySQL 会执行接收到的 SQL 并将执行结果通过网络返回。要实现这项操作,需要先通过连接对象的
cursor方法获取游标(Cursor)对象。
- 发出 SQL。通过游标对象的
execute方法,我们可以向数据库发出 SQL 语句。
- 如果执行
insert、delete或update操作,需要根据实际情况提交或回滚事务。因为创建连接时,默认开启了事务环境,在操作完成后,需要使用连接对象的commit或rollback方法,实现事务的提交或回滚,rollback方法通常会放在异常捕获代码块except中。如果执行select操作,需要通过游标对象抓取查询的结果,对应的方法有三个,分别是:fetchone、fetchmany和fetchall。其中fetchone方法会抓取到一条记录,并以元组或字典的方式返回;fetchmany和fetchall方法会抓取到多条记录,以嵌套元组或列表装字典的方式返回。
- 关闭连接。在完成持久化操作后,请不要忘记关闭连接,释放外部资源。我们通常会在
finally代码块中使用连接对象的close方法来关闭连接。
代码实操
下面,我们通过代码实操的方式为大家演示上面说的五个步骤。
插入数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| import pymysql
no = int(input('部门编号: ')) name = input('部门名称: ') location = input('部门所在地: ')
conn = pymysql.connect(host='127.0.0.1', port=3306, user='guest', password='Guest.618', database='hrs', charset='utf8mb4') try: with conn.cursor() as cursor: affected_rows = cursor.execute( 'insert into `tb_dept` values (%s, %s, %s)', (no, name, location) ) if affected_rows == 1: print('新增部门成功!!!') conn.commit() except pymysql.MySQLError as err: conn.rollback() print(type(err), err) finally: conn.close()
|
说明:上面的127.0.0.1称为回环地址,它代表的是本机。下面的guest是我提前创建好的用户,该用户拥有对hrs数据库的insert、delete、update和select权限。我们不建议大家在项目中直接使用root超级管理员账号访问数据库,这样做实在是太危险了。我们可以使用下面的命令创建名为guest的用户并为其授权。
1 2
| create user 'guest'@'%' identified by 'Guest.618'; grant insert, delete, update, select on `hrs`.* to 'guest'@'%';
|
如果要插入大量数据,建议使用游标对象的executemany方法做批处理(一个insert操作后面跟上多组数据),大家可以尝试向一张表插入10000条记录,然后看看不使用批处理一条条的插入和使用批处理有什么差别。游标对象的executemany方法第一个参数仍然是 SQL 语句,第二个参数可以是包含多组数据的列表或元组。
删除数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| import pymysql
no = int(input('部门编号: '))
conn = pymysql.connect(host='127.0.0.1', port=3306, user='guest', password='Guest.618', database='hrs', charset='utf8mb4', autocommit=True) try: with conn.cursor() as cursor: affected_rows = cursor.execute( 'delete from `tb_dept` where `dno`=%s', (no, ) ) if affected_rows == 1: print('删除部门成功!!!') finally: conn.close()
|
说明:如果不希望每次 SQL 操作之后手动提交或回滚事务,可以connect函数中加一个名为autocommit的参数并将它的值设置为True,表示每次执行 SQL 成功后自动提交。但是我们建议大家手动提交或回滚,这样可以根据实际业务需要来构造事务环境。如果不愿意捕获异常并进行处理,可以在try代码块后直接跟finally块,省略except意味着发生异常时,代码会直接崩溃并将异常栈显示在终端中。
更新数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| import pymysql
no = int(input('部门编号: ')) name = input('部门名称: ') location = input('部门所在地: ')
conn = pymysql.connect(host='127.0.0.1', port=3306, user='guest', password='Guest.618', database='hrs', charset='utf8mb4') try: with conn.cursor() as cursor: affected_rows = cursor.execute( 'update `tb_dept` set `dname`=%s, `dloc`=%s where `dno`=%s', (name, location, no) ) if affected_rows == 1: print('更新部门信息成功!!!') conn.commit() except pymysql.MySQLError as err: conn.rollback() print(type(err), err) finally: conn.close()
|
查询数据
- 查询部门表的数据。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| import pymysql
conn = pymysql.connect(host='127.0.0.1', port=3306, user='guest', password='Guest.618', database='hrs', charset='utf8mb4') try: with conn.cursor() as cursor: cursor.execute('select `dno`, `dname`, `dloc` from `tb_dept`') row = cursor.fetchone() while row: print(row) row = cursor.fetchone() except pymysql.MySQLError as err: print(type(err), err) finally: conn.close()
|
说明:上面的代码中,我们通过构造一个while循环实现了逐行抓取查询结果的操作。这种方式特别适合查询结果有非常多行的场景。因为如果使用fetchall一次性将所有记录抓取到一个嵌套元组中,会造成非常大的内存开销,这在很多场景下并不是一个好主意。如果不愿意使用while循环,还可以考虑使用iter函数构造一个迭代器来逐行抓取数据,有兴趣的读者可以自行研究。
- 分页查询员工表的数据。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| import pymysql
page = int(input('页码: ')) size = int(input('大小: '))
con = pymysql.connect(host='127.0.0.1', port=3306, user='guest', password='Guest.618', database='hrs', charset='utf8') try: with con.cursor(pymysql.cursors.DictCursor) as cursor: cursor.execute( 'select `eno`, `ename`, `job`, `sal` from `tb_emp` order by `sal` desc limit %s,%s', ((page - 1) * size, size) ) for emp_dict in cursor.fetchall(): print(emp_dict) finally: con.close()
|
案例讲解
下面我们为大家讲解一个将数据库表数据导出到 Excel 文件的例子,我们需要先安装openpyxl三方库,命令如下所示。
接下来,我们通过下面的代码实现了将数据库hrs中所有员工的编号、姓名、职位、月薪、补贴和部门名称导出到一个 Excel 文件中。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| import openpyxl import pymysql
workbook = openpyxl.Workbook()
sheet = workbook.active
sheet.title = '员工基本信息'
sheet.append(('工号', '姓名', '职位', '月薪', '补贴', '部门'))
conn = pymysql.connect(host='127.0.0.1', port=3306, user='guest', password='Guest.618', database='hrs', charset='utf8mb4') try: with conn.cursor() as cursor: cursor.execute( 'select `eno`, `ename`, `job`, `sal`, coalesce(`comm`, 0), `dname` ' 'from `tb_emp` natural join `tb_dept`' ) row = cursor.fetchone() while row: sheet.append(row) row = cursor.fetchone() workbook.save('hrs.xlsx') except pymysql.MySQLError as err: print(err) finally: conn.close()
|
大家可以参考上面的例子,试一试把 Excel 文件的数据导入到指定数据库的指定表中,看看是否可以成功。