適用環境
python版本 >=2.6或3.3
mysql版本>=4.1
安裝
可以使用pip安裝也可以手動下載安裝。
使用pip安裝,在命令行執行如下命令:
1
|
pip install PyMySQL
|
手動安裝,請先下載。下載地址:https://github.com/PyMySQL/PyMySQL/tarball/pymysql-X.X。
其中的X.X是版本(目前可以獲取的最新版本是0.6.6)。
下載后解壓壓縮包。在命令行中進入解壓后的目錄,執行如下的指令:
1
|
python setup.py install
|
建議使用pip安裝。
使用示例
連接數據庫如下:
1
2
3
4
5
6
7
8
9
10
|
import pymysql.cursors
# Connect to the database
connection = pymysql.connect(host='127.0.0.1',
port=3306,
user='root',
password='zhyea.com',
db='employees',
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor)
|
也可以使用字典進行連接參數的管理,我覺得這樣子更優雅一些:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
import pymysql.cursors
config = {
'host':'127.0.0.1',
'port':3306,
'user':'root',
'password':'zhyea.com',
'db':'employees',
'charset':'utf8mb4',
'cursorclass':pymysql.cursors.DictCursor,
}
# Connect to the database
connection = pymysql.connect(**config)
|
插入數據:
執行sql語句前需要獲取cursor,因為配置默認自動提交,故在執行sql語句后需要主動commit,最后不要忘記關閉連接:
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
|
from datetime import date, datetime, timedelta
import pymysql.cursors
#連接配置信息
config = {
'host':'127.0.0.1',
'port':3306,
'user':'root',
'password':'zhyea.com',
'db':'employees',
'charset':'utf8mb4',
'cursorclass':pymysql.cursors.DictCursor,
}
# 創建連接
connection = pymysql.connect(**config)
# 獲取明天的時間
tomorrow = datetime.now().date() + timedelta(days=1)
# 執行sql語句
try:
with connection.cursor() as cursor:
# 執行sql語句,插入記錄
sql = 'INSERT INTO employees (first_name, last_name, hire_date, gender, birth_date) VALUES (%s, %s, %s, %s, %s)'
cursor.execute(sql, ('Robin', 'Zhyea', tomorrow, 'M', date(1989, 6, 14)));
# 沒有設置默認自動提交,需要主動提交,以保存所執行的語句
connection.commit()
finally:
connection.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
30
31
32
33
34
|
import datetime
import pymysql.cursors
#連接配置信息
config = {
'host':'127.0.0.1',
'port':3306,
'user':'root',
'password':'zhyea.com',
'db':'employees',
'charset':'utf8mb4',
'cursorclass':pymysql.cursors.DictCursor,
}
# 創建連接
connection = pymysql.connect(**config)
# 獲取雇佣日期
hire_start = datetime.date(1999, 1, 1)
hire_end = datetime.date(2016, 12, 31)
# 執行sql語句
try:
with connection.cursor() as cursor:
# 執行sql語句,進行查詢
sql = 'SELECT first_name, last_name, hire_date FROM employees WHERE hire_date BETWEEN %s AND %s'
cursor.execute(sql, (hire_start, hire_end))
# 獲取查詢結果
result = cursor.fetchone()
print(result)
# 沒有設置默認自動提交,需要主動提交,以保存所執行的語句
connection.commit()
finally:
connection.close();
|
這里的查詢支取了一條查詢結果,查詢結果以字典的形式返回:
從結果集中獲取指定數目的記錄,可以使用fetchmany方法:
1
|
result = cursor.fetchmany(2)
|
不過不建議這樣使用,最好在sql語句中設置查詢的記錄總數。
獲取全部結果集可以使用fetchall方法:
1
|
result = cursor.fetchall()
|
因為只有兩條記錄,所以上面提到的這兩種查詢方式查到的結果是一樣的:
1
|
[{'last_name': 'Vanderkelen', 'hire_date': datetime.date(2015, 8, 12), 'first_name': 'Geert'}, {'last_name': 'Zhyea', 'hire_date': datetime.date(2015, 8, 21), 'first_name': 'Robin'}]
|
在django中使用
在django中使用是我找這個的最初目的。目前同時支持python3.4、django1.8的數據庫backend並不好找。這個是我目前找到的最好用的。
設置DATABASES和官方推薦使用的MySQLdb的設置沒什么區別:
1
2
3
4
5
6
7
8
9
10
|
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'mytest',
'USER': 'root',
'PASSWORD': 'zhyea.com',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
|
關鍵是這里:我們還需要在站點的__init__.py文件中添加如下的內容:
1
2
|
import pymysql
pymysql.install_as_MySQLdb()
|