PyMySQL库使用详解

PyMySQL简介

一个比较方便的连接mysql使用的python库,官网给的例子很简单,但是看下源码发现内容还是很多的,很多函数都没有介绍,所以只有在使用的时候查看源代码了。从github上该项目所获得的星数来看,该库还是很出名的。

PyMySQL使用

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
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()

结果:

1
{'password': 'very-secret', 'id': 1}

参考文档

坚持原创技术分享,您的支持将鼓励我继续创作!