主要簡單說下Python 3.3搭配MySQL Community Server 5.6的使用。在Python 3系列和MySQL 5.0系列裏面下面的代碼應該都通用。(沒有驗證)html
準備
python 3 這個在python的官方網站就能download. http://www.python.org/python
MySQL Community Server 5.6 在MySQL的官網也能找到: http://dev.mysql.com/downloads/mysql/5.6.htmlmysql
MySQL官方已經提供了支持Python 3.0的connector,地址: http://dev.mysql.com/downloads/connector/ (這裏包含了MySQL對C, C++, Python, Java, .NET等語言的鏈接器)sql
簡述
Python3訪問MySQL 5.6過程和訪問其餘的數據沒有太大的不一樣。都經歷如下幾個步驟:數據庫
- 創建鏈接。
- 創建遊標cursor。
- 調用cursor的execute方法,以SQL語句和變量爲參數,執行數據庫操做。
- 返回結果。
- 關閉遊標,關閉鏈接。
在操做以前,看下MySQL數據庫裏面的數據結構安全
讀數據
從數據庫讀數據的基本操做。基本就是遵循上面的步驟。數據結構
eg1>>> import mysql
>>> from mysql import connector
>>> user = 'herbert'
>>> pwd = '851020'
>>> host = '127.0.0.1'
>>> db = 'world'
>>> cnx = mysql.connector.connect(user=user, password=pwd, host=host,
database=db)
>>> cursor = cnx.cursor()
>>> cursor.execute("SELECT * FROM CITY WHERE COUNTRYCODE = 'CHN';")
>>> cities = cursor.fetchall()
>>> cities
[(1890, 'Shanghai', 'CHN', 'Shanghai', 9696300), (1891, 'Peking', 'CHN', 'Peking', 7472000), (1892, 'Chongqing', 'CHN', 'Chongqing', 6351600), (1893, 'Tianjin', 'CHN', 'Tianjin', 5286800), ....
最後調用相應的方法關閉鏈接。裏面關於Exception的處理方法和通常數據庫鏈接類似,可是python裏面每一個數據庫都定義了本身的DatabaseError。對於MySQL的DatabaseError是定義在mysql.connector模塊中的。能夠經過mysql.connector來查看和使用。fetch
eg2>>> cursor.close()
True
>>> cnx.close()
>>> cnx
<mysql.connector.connection.MySQLConnection object at 0x00000000037FF160>
>>> cnx.is_connected()
False
>>>
寫數據
如何更新數據庫數據或者插入新數據。寫入完成後,最好調用下鏈接器的commit()提交下數據(雖然它也會自動commit)網站
eg3>>> import mysql
>>> from mysql import connector
>>> user = 'herbert'
>>> pwd = '*****'
>>> host = '127.0.0.1'
>>> db = 'world'
>>> cnx = mysql.connector.connect(user=user, password=pwd, host=host,
database=db)
>>> cursor = cnx.cursor()
>>> cursor.execute("Update city Set Population = 9696500 Where ID = 1890 AND \
NAME = 'Shanghai';")
>>> cursor.close()
True
>>> cnx.commit()
>>> cnx.is_connected()
True
>>> cnx.close()
>>> cnx.is_connected()
False
>>>
更新後的數據庫數據this
參數化
在實際使用過程當中,SQL語句的使用每每會帶有參數,不是固定的,會接受用戶的參數來返回用戶想要的結果。同時這樣作也能夠防範一些SQL注入的危險。
在python的數據庫模塊中,每種數據庫對應的參數處理形式每每是不一樣的。要根據具體的數據庫的paramstyle選用相應的參數傳入形式。有如下幾種形式:
- qmark 問號類型。 eg. cur.execute(「… where name = ? and account = ?」, (symbol, account))
- numberic 數字類型。eg. cur.execute(「… where name = :0 and account = :1」, (symbol, account))
- named 命名類型。ORACLE數據庫採用的形式。eg. cur.execute(「… where name = : symbol and account = :account」, {‘symbol’:value1, ‘account’: value2})
- format 格式代碼。eg. cur.execute(「… where name = %s and account = %d」, (symbol, account))
- pyformat 擴展格式代碼。MySQL採用的就是這種形式。eg. cur.execute(「… where name = %(name)s and account = %(account)d」, {‘symbol’:value1, ‘account’: value2})
paramstyle查看:
eg5>>> mysql.connector.paramstyle
'pyformat'
示例
eg4>>> import mysql
>>> from mysql import connector
>>> user = 'herbert'
>>> pwd = '851020'
>>> host = '127.0.0.1'
>>> db = 'world'
>>> country = 'CHN'
>>> cnx = mysql.connector.connect(user=user, password=pwd, host=host,
database=db)
>>> cursor = cnx.cursor()
>>> cursor.execute("SELECT * FROM CITY where CountryCode = %(country)s", {'country':country})
>>>
上面的示例一樣也適用與更新和寫入新數據操做。這樣比字符串拼接有更好的安全性。
額外注意
- paramstyle在不一樣的數據庫上的不一樣。
- cursor.execute方法在不一樣的數據庫上適用略有不一樣。好比在ORACLE數據庫上,還要先調用其cursor.prepare()方法,傳入SQL語句。
- 對於數據庫操做過程當中的各類exception的處理,包括更新和寫操做成功性的判斷,進行commit或者rollback操做。
- 對於數據庫讀取的返回的數據的處理,可使用namedtuple.可是還有更好的方法,在後面的文章進行專門講述。
- 關於cursor的其餘的方法的使用。python操做數據庫最關鍵的仍是cursor的各類方法調用。