Mac 下Python 連接MySQL及使用
Mac 下Python 連接MySQL及使用 安裝
首先需要安裝mysql或mariadb:
brew mysql(或brew mariadb)
下載Python連接MySQL的接口模塊MySQLdb:
地址:https://pypi.python.org/pypi/MySQL-python/1.2.5
下載zip包后解壓出來,進(jìn)入目錄,使用命令安裝:(詳見目錄下INSTALL文件)
python setup.py build
sudo python setup.py install # or su first
之后即可在Python中使用該模塊: import MySQLdb
centos下直接yum install MySQL-python即可。
使用 連接
#!/usr/bin/python
#?-*-?coding:?UTF-8?-*-
import?MySQLdb
#?打開數(shù)據(jù)庫連接
db?=?MySQLdb.connect("localhost","testuser","test123","TESTDB"?)
#?使用cursor()方法獲取操作游標(biāo)?
cursor?=?db.cursor()
#?使用execute方法執(zhí)行SQL語句
cursor.execute("SELECT?VERSION()")
#?使用?fetchone()?方法獲取一條數(shù)據(jù)庫。
data?=?cursor.fetchone()
print?"Database?version?:?%s?"?%?data
#?關(guān)閉數(shù)據(jù)庫連接
db.close()創(chuàng)建數(shù)據(jù)庫表
如果數(shù)據(jù)庫連接存在我們可以使用execute()方法來為數(shù)據(jù)庫創(chuàng)建表,如下所示創(chuàng)建表EMPLOYEE:
#!/usr/bin/python
#?-*-?coding:?UTF-8?-*-
import?MySQLdb
#?打開數(shù)據(jù)庫連接
db?=?MySQLdb.connect("localhost","testuser","test123","TESTDB"?)
#?使用cursor()方法獲取操作游標(biāo)?
cursor?=?db.cursor()
#?如果數(shù)據(jù)表已經(jīng)存在使用?execute()?方法刪除表。
cursor.execute("DROP?TABLE?IF?EXISTS?EMPLOYEE")
#?創(chuàng)建數(shù)據(jù)表SQL語句
sql?=?"""CREATE?TABLE?EMPLOYEE?(
?????????FIRST_NAME??CHAR(20)?NOT?NULL,
?????????LAST_NAME??CHAR(20),
?????????AGE?INT,??
?????????SEX?CHAR(1),
?????????INCOME?FLOAT?)"""
cursor.execute(sql)
#?關(guān)閉數(shù)據(jù)庫連接
db.close()數(shù)據(jù)庫插入
#!/usr/bin/python
#?-*-?coding:?UTF-8?-*-
import?MySQLdb
#?打開數(shù)據(jù)庫連接
db?=?MySQLdb.connect("localhost","testuser","test123","TESTDB"?)
#?使用cursor()方法獲取操作游標(biāo)?
cursor?=?db.cursor()
#?SQL?插入語句
sql?=?"""INSERT?INTO?EMPLOYEE(FIRST_NAME,
?????????LAST_NAME,?AGE,?SEX,?INCOME)
?????????VALUES?('Mac',?'Mohan',?20,?'M',?2000)"""
try:
???#?執(zhí)行sql語句
???cursor.execute(sql)
???#?提交到數(shù)據(jù)庫執(zhí)行
???db.commit()
except:
???#?Rollback?in?case?there?is?any?error
???db.rollback()
#?關(guān)閉數(shù)據(jù)庫連接
db.close()或者寫成:
#!/usr/bin/python
#?-*-?coding:?UTF-8?-*-
import?MySQLdb
#?打開數(shù)據(jù)庫連接
db?=?MySQLdb.connect("localhost","testuser","test123","TESTDB"?)
#?使用cursor()方法獲取操作游標(biāo)?
cursor?=?db.cursor()
#?SQL?插入語句
sql?=?"INSERT?INTO?EMPLOYEE(FIRST_NAME,?
???????LAST_NAME,?AGE,?SEX,?INCOME)?
???????VALUES?('%s',?'%s',?'%d',?'%c',?'%d'?)"?%?
???????('Mac',?'Mohan',?20,?'M',?2000)
try:
???#?執(zhí)行sql語句
???cursor.execute(sql)
???#?提交到數(shù)據(jù)庫執(zhí)行
???db.commit()
except:
???#?發(fā)生錯(cuò)誤時(shí)回滾
???db.rollback()
#?關(guān)閉數(shù)據(jù)庫連接
db.close()傳入?yún)?shù):
user_id?=?"test123"
password?=?"password"
con.execute('insert?into?Login?values("%s",?"%s")'?%?
?????????????(user_id,?password))查詢
Python查詢Mysql使用 fetchone() 方法獲取單條數(shù)據(jù), 使用fetchall() 方法獲取多條數(shù)據(jù)。
fetchone(): 該方法獲取下一個(gè)查詢結(jié)果集。結(jié)果集是一個(gè)對(duì)象 fetchall():接收全部的返回結(jié)果行. rowcount: 這是一個(gè)只讀屬性,并返回執(zhí)行execute()方法后影響的行數(shù)。
#!/usr/bin/python
#?-*-?coding:?UTF-8?-*-
import?MySQLdb
#?打開數(shù)據(jù)庫連接
db?=?MySQLdb.connect("localhost","testuser","test123","TESTDB"?)
#?使用cursor()方法獲取操作游標(biāo)?
cursor?=?db.cursor()
#?SQL?查詢語句
sql?=?"SELECT?*?FROM?EMPLOYEE?
???????WHERE?INCOME?>?'%d'"?%?(1000)
try:
???#?執(zhí)行SQL語句
???cursor.execute(sql)
???#?獲取所有記錄列表
???results?=?cursor.fetchall()
???for?row?in?results:
??????fname?=?row[0]
??????lname?=?row[1]
??????age?=?row[2]
??????sex?=?row[3]
??????income?=?row[4]
??????#?打印結(jié)果
??????print?"fname=%s,lname=%s,age=%d,sex=%s,income=%d"?%?
?????????????(fname,?lname,?age,?sex,?income?)
except:
???print?"Error:?unable?to?fecth?data"
#?關(guān)閉數(shù)據(jù)庫連接
db.close()執(zhí)行事務(wù)
#?SQL刪除記錄語句 sql?=?"DELETE?FROM?EMPLOYEE?WHERE?AGE?>?'%d'"?%?(20) try: ???#?執(zhí)行SQL語句 ???cursor.execute(sql) ???#?向數(shù)據(jù)庫提交 ???db.commit() except: ???#?發(fā)生錯(cuò)誤時(shí)回滾 ???db.rollback()
參考資料:
《在 Mac 下用 Homebrew 安裝 MySQL》http://blog.neten.de/posts/2014/01/27/install-mysql-using-homebrew/
?《python操作mysql數(shù)據(jù)庫》http://www.runoob.com/python/python-mysql.html





