添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

PyMySQL 教程 展示了如何使用 PyMySQL 模块在 Python 中对 MySQL 进行编程。

PyMySQL

PyMySQL 是基于 PEP 249 的纯 Python MySQL 客户端库。大多数公共 API 与 mysqlclient 和 MySQLdb 兼容。 PyMySQL 可与 MySQL 5.5+和 MariaDB 5.5+一起使用。

MySQL 是领先的开源数据库管理系统。 它是一个多用户,多线程的数据库管理系统。 MySQL 在网络上特别流行。

PyMySQL 安装

1
$ sudo pip3 install PyMySQL

我们使用pip3工具安装 PyMySQL。

PyMySQL 版本示例

在下面的示例中,我们获取 MySQL 的版本。

version.py 16
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import pymysql
con = pymysql.connect('localhost', 'user17', 
    's$cret', 'mydb')
with con:
    cur = con.cursor()
    cur.execute("SELECT VERSION()")
    version = cur.fetchone()
    print("Database version: {}".format(version[0]))

在 MySQL 中,我们可以使用SELECT VERSION()获取 MySQL 的版本。

con = pymysql.connect('localhost', 'user17', 
    's$cret', 'mydb')

我们使用connect()连接到数据库。 我们传递四个参数:主机名,MySQL 用户名,密码和数据库名。

with con:

使用with关键字,Python 解释器会自动释放资源。 它还提供错误处理。

cur = con.cursor()

从连接对象,我们创建一个游标。 游标用于遍历结果集中的记录。

cur.execute("SELECT VERSION()")

我们调用游标的execute()方法并执行 SQL 语句。

version = cur.fetchone()

fetchone()方法获取查询结果集的下一行,返回单个序列,或者在没有更多数据可用时返回None

$ ./version.py 
Database version: 5.7.23-0ubuntu0.16.04.1

这是输出。

PyMySQL fetchAll

fetchAll()方法检索查询结果的所有(剩余)行,并将它们作为序列序列返回。

retrieve_all.py 17
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import pymysql
con = pymysql.connect('localhost', 'user17', 
    's$cret', 'testdb')
with con: 
    cur = con.cursor()
    cur.execute("SELECT * FROM cities")
    rows = cur.fetchall()
    for row in rows:
        print("{0} {1} {2}".format(row[0], row[1], row[2]))

在示例中,我们从数据库表中检索所有城市。

cur.execute("SELECT * FROM cities")

该 SQL 语句从 citys 表中选择所有数据。

$ ./retrieve_all.py 
1 Bratislava 432000
2 Budapest 1759000
3 Prague 1280000
4 Warsaw 1748000
5 Los Angeles 3971000
6 New York 8550000
7 Edinburgh 464000
8 Berlin 3671000

这是输出。

PyMySQL 字典游标

默认游标以元组的元组返回数据。 当我们使用字典游标时,数据以 Python 字典的形式发送。 这样,我们可以通过列名称来引用数据。

dictionary_cursor.py 22
for row in rows:
    print(row["id"], row["name"])

我们通过“城市”表的列名称引用数据。

PyMySQL 列标题

接下来,我们将展示如何使用数据库表中的数据打印列标题。

column_headers.py 21
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import pymysql
con = pymysql.connect('localhost', 'user17', 
    's$cret', 'testdb')
with con:
    cur = con.cursor()
    cur.execute("SELECT * FROM cities")
    rows = cur.fetchall()
    desc = cur.description
    print("{0:>3} {1:>10}".format(desc[0][0], desc[1][0]))
    for row in rows:    
        print("{0:3} {1:>10}".format(row[0], row[2]))

列名称被认为是元数据。 它们是从游标对象获得的。

desc = cur.description

游标的description属性返回有关查询的每个结果列的信息。

$ ./column_headers.py 
 id       name
  1     432000
  2    1759000
  3    1280000
  4    1748000
  5    3971000
  6    8550000
  7     464000
  8    3671000

这是输出。

PyMySQL 预备语句

在编写预备语句时,我们使用占位符,而不是直接将值写入语句中。 预准备的语句可提高安全性和性能。

prepared_statement.py 19
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import pymysql
con = pymysql.connect('localhost', 'user17', 
    's$cret', 'testdb')
# user input
myid = 4
with con:    
    cur = con.cursor()
    cur.execute("SELECT * FROM cities WHERE id=%s", myid) 
    cid, name, population  = cur.fetchone()
    print(cid, name, population)

在示例中,我们获得具有指定 ID 的行。