Python+MySQL学习笔记(一)
Python其實(shí)是可以操縱數(shù)據(jù)庫的,想想如果能在python里寫SQL語句,從而直接達(dá)到操縱數(shù)據(jù)庫的目的,how cool!
Python的標(biāo)準(zhǔn)數(shù)據(jù)庫接口為Python DB-API,針對(duì)不同的數(shù)據(jù)庫要安裝不同的模塊,其中,MySQLdb就是用于鏈接MySQL數(shù)據(jù)庫的接口。
安裝MySQLdb,可以在?http://sourceforge.net/projects/mysql-python?上找到安裝包,下載安裝即可,無需多余的設(shè)置。
使用流程大致如下:
接下里分開記錄數(shù)據(jù)庫連接、創(chuàng)建數(shù)據(jù)庫表、數(shù)據(jù)庫插入操作、查詢操作、更新操作、刪除操作和錯(cuò)誤處理
?
| # 導(dǎo)入模塊 import MySQLdb # 數(shù)據(jù)庫連接 db = MySQLdb.connect(host= "localhost", user='****', passwd='****', db='****') |
?
數(shù)據(jù)庫連接比較簡(jiǎn)單,格式也都是固定的,注意在連接前,確保用戶擁有操縱某個(gè)數(shù)據(jù)庫的權(quán)限
?
| # -*- coding:utf-8 -*- import MySQLdb db = MySQLdb.connect(host = 'localhost', user = 'wpy', passwd = '2016', db = 'testdb') # 獲取游標(biāo) cursor = db.cursor() # cursor的execute方法可以執(zhí)行SQL # 如果表EMPLOYEE存在則刪除 cursor.execute('DROP TABLE IF EXISTS EMPLOYEE') ? sql = 'CREATE TABLE EMPLOYEE(\ NAME VARCHAR(10) PRIMARY KEY,\ AGE INT,\ SEX VARCHAR(1))' cursor.execute(sql) # 關(guān)閉游標(biāo) cursor.close() # 關(guān)閉數(shù)據(jù)庫連接 db.close() |
?
在powershell中查看:
?
?
沒有問題!
?
?
| # -*- coding:utf-8 -*- import MySQLdb db = MySQLdb.connect(host = 'localhost', user = 'wpy', passwd = '2016', db = 'testdb') # 獲取游標(biāo) cursor = db.cursor() ? # 插入一條記錄 sql = "INSERT INTO EMPLOYEE(NAME, AGE, SEX) VALUES ('Simon', 20, 'M')" try: cursor.execute(sql) # 一定要commit()一下數(shù)據(jù)庫才會(huì)被更新 db.commit() except: db.rollback() ? # 插入多條記錄 values = [('Lucy', 21, 'W'), ('Tom', 19, 'M')] # 必須是%s,且沒有引號(hào),%d是不行的 sql = "INSERT INTO EMPLOYEE(NAME, AGE, SEX) VALUES (%s, %s, %s)" try: cursor.executemany(sql, values) db.commit() except: db.rollback() ? # 關(guān)閉游標(biāo) cursor.close() # 關(guān)閉數(shù)據(jù)庫連接 db.close() |
?
結(jié)果如下:
?
?
?
| # -*- coding:utf-8 -*- import MySQLdb db = MySQLdb.connect(host = 'localhost', user = 'wpy', passwd = '2016', db = 'testdb') # 獲取游標(biāo) cursor = db.cursor() ? # SQL查詢語句 sql = "SELECT * FROM EMPLOYEE WHERE AGE >= '%d'" % (20) ? try: cursor.execute(sql) # 獲取所有記錄 rows = cursor.fetchall() for row in rows: print 'Name:', row[0], 'Age:', row[1], 'Sex:', row[2] except: print 'Error, return nothing.' # 關(guān)閉游標(biāo) cursor.close() # 關(guān)閉數(shù)據(jù)庫連接 db.close() |
?
結(jié)果如下:
?
Cursor還有兩個(gè)方法,fetchone()和fetchmany(size=)
fetchmany(size=)比較簡(jiǎn)單,可以控制返回的行數(shù)
fetchone()的意義在于逐行獲取,因?yàn)槊渴褂靡淮蝔etch,游標(biāo)的位置就會(huì)發(fā)生改變,結(jié)果也看得出來,不過這也正是游標(biāo)的意義所在了
?
?
| # -*- coding:utf-8 -*- import MySQLdb db = MySQLdb.connect(host = 'localhost', user = 'wpy', passwd = '2016', db = 'testdb') # 獲取游標(biāo) cursor = db.cursor() ? # SQL更新語句 sql = "UPDATE EMPLOYEE SET AGE = AGE + 1 WHERE AGE >= '%d'" % (20) ? try: cursor.execute(sql) db.commit() except: db.rollback() # 關(guān)閉游標(biāo) cursor.close() # 關(guān)閉數(shù)據(jù)庫連接 db.close() |
?
數(shù)據(jù)庫更新操作還是比較簡(jiǎn)單,注意別忘了db.commit()
?
| # -*- coding:utf-8 -*- import MySQLdb db = MySQLdb.connect(host = 'localhost', user = 'wpy', passwd = '2016', db = 'testdb') # 獲取游標(biāo) cursor = db.cursor() ? # SQL刪除語句 sql = "DELETE FROM EMPLOYEE WHERE AGE < '%d'" % (20) ? try: cursor.execute(sql) db.commit() except: db.rollback() # 關(guān)閉游標(biāo) cursor.close() # 關(guān)閉數(shù)據(jù)庫連接 db.close() |
?
比較簡(jiǎn)單,不多說
?
總結(jié):MySQLdb的語法還是比較簡(jiǎn)單的,步驟也很單一,除了SQL不同之外,整體框架大體上沒有發(fā)生太大的改變。還是那句話,SQL是根本,只有掌握好SQL才能執(zhí)行更為復(fù)雜的操作。
其實(shí)MySQLdb的語法遠(yuǎn)不止這些,不過這些就已經(jīng)夠用,如果想深入學(xué)習(xí),可以參考官方網(wǎng)站http://dev.mysql.com/doc/connector-python/en/
?
參考資料:http://www.runoob.com/python/python-mysql.html
? ? ? ? ? ? ? ?http://www.cnblogs.com/coser/archive/2012/01/12/2320741.html
? ? ? ? ? ? ? ?http://blog.csdn.net/lengyue318/article/details/7913427
轉(zhuǎn)載于:https://www.cnblogs.com/lucifer25/p/5782007.html
總結(jié)
以上是生活随笔為你收集整理的Python+MySQL学习笔记(一)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 特定页面隐藏导航栏
- 下一篇: hive与hbase整合方式和优劣