PyMySQL 用户指南解释了如何安装 PyMySQL 以及如何作为开发人员为库做贡献。
最后一个稳定版本可在PyPI上使用,可以安装 pip:
$ python3 -m pip install PyMySQL
要使用 sha256_password 或 caching_sha2_password 进行身份验证,您需要安装其他依赖项:
$ python3 -m pip install PyMySQL[rsa]
以下示例使用简单表
"""
CREATE TABLE users(
id int(11) NOT NULL AUTO_INCREMENT,
email varchar(255) COLLATE utf8_bin NOT NULL,
password varchar(255) COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`id`)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin
AUTO_INCREMENT=1 ;
"""
import pymysql.cursors
# Connect to the database
connection = pymysql.connect(host='localhost',
user='user',
password='passwd',
db='db',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
# Create a new record
sql = "INSERT INTO `users` (`email`, `password`) VALUES (%s, %s)"
cursor.execute(sql, ('webmaster@python.org', 'very-secret'))
# connection is not autocommit by default. So you must commit to save
# your changes.
connection.commit()
with connection.cursor() as cursor:
# Read a single record
sql = "SELECT `id`, `password` FROM `users` WHERE `email`=%s"
cursor.execute(sql, ('webmaster@python.org',))
result = cursor.fetchone()
print(result)
finally:
connection.close()
这个例子将打印:
{'password': 'very-secret', 'id': 1}
如果您想运行测试套件,请创建一个用于测试的数据库,如下所示:
mysql -e 'create database test_pymysql DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;'
mysql -e 'create database test_pymysql2 DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;'
然后,将文件复制.travis/database.json到pymysql/tests/databases.json 并编辑新文件以匹配您的MySQL配置:
$ cp .travis/database.json pymysql/tests/databases.json
$ $EDITOR pymysql/tests/databases.json
要运行所有测试,请执行以下脚本runtests.py:
$ python runtests.py
tox.ini还提供了一个文件,用于在多个Python版本上方便地运行测试:
$ tox
如果您正在寻找有关特定功能,类或方法的信息,本文档的这一部分适合您。
有关更多信息,请阅读 Python 数据库 API 规范( peps.python 组织网/pep-0249/)。
class pymysql.connections.Connection(
host=None,
user=None,
password='',
database=None,
port=0,
unix_socket=None,
charset='',
sql_mode=None,
read_default_file=None,
conv=None,
use_unicode=None,
client_flag=0,
cursorclass=<class'pymysql.cursors.Cursor'>,
init_command=None,
connect_timeout=10,
ssl=None,
read_default_group=None,
compress=None,
named_pipe=None,
autocommit=False,
db=None,
passwd=None,
local_infile=False,
max_allowed_packet=16777216,
defer_connect=False,
auth_plugin_map=None,
read_timeout=None,
write_timeout=None,
bind_address=None,
binary_prefix=False,
program_name=None,
server_public_key=None
)
用 MySQL 服务器表示套接字。获取此类实例的正确方法是调用 connect()。建立与MySQL数据库的连接。接受几个论点
请参阅规范中的 连接。
class pymysql.cursors.Cursor (连接)
兼容性警告:PEP-249 指定必须返回任何已修改的参数。这当前是不可能的,因为它们只能通过将它们存储在服务器变量中然后通过查询检索来获得。由于存储过程返回零个或多个结果集,因此没有可靠的方法通过 callproc 获取 OUT 或 INOUT 参数。服务器变量名为 @_procname_n,其中 procname 是上面的参数,n 是参数的位置(从零开始)。获取过程生成的所有结果集后,可以使用.execute()发出 SELECT @ _procname_0,... 查询以获取任何 OUT 或 INOUT 值。
兼容性警告:调用存储过程本身的行为会创建一个空结果集。在程序生成的任何结果集之后出现。这是关于DB-API的非标准行为。一定要使用nextset()来推进所有结果集; 否则你可能会断开连接。
返回:受影响的行数
返回类型:INT
如果args是dict,则%(name)s可以用作查询中的占位符。
针对一个查询运行多个数据
参数:
> **query** - 要在服务器上执行的查询 >
args - 序列或映射的序列。它用作参数。
返回:受影响的行数(如果有)。
此方法可提高多行INSERT和REPLACE的性能。否则它等同于使用execute()循环遍历args。
获取所有行
获取几行
获取下一行
executemany() 生成的最大语句大小。允许语句的最大大小为max_allowed_packet - packet_header_size。max_allowed_packet的默认值是1048576。
通过调用execute()方法返回发送到数据库的确切字符串。此方法遵循DB API 2.0的扩展,然后是Psycopg。
没有,DB API 要求。
没有,DB API 要求。
class pymysql.cursors.SSCursor (连接)
class pymysql.cursors.DictCursor(连接)
class pymysql.cursors.SSDictCursor (连接)

