首先你需要安裝上MySQL和MySQLdb模塊(當然還有其他模塊可以用),這里我就略過了,如果遇到問題自行百度(或者評論在下面我可以幫忙看看)
這里簡單記錄一下自己使用的學習過程:
一、連接數據庫
MySQLdb提供了connect函數,使用如下
cxn=MySQLdb.Connect(host='localhost',user='root',passwd='',db='samp_db',port=3306)
這里的參數的意義都是很明確的,但是這些參數并不是都是必須的:
1、host參數表示的是數據庫所在地址,默認值是localhost,也就是說本機運行這個參數可以不指定
2、user、passwd 數據庫的用戶名和密碼,必須存在
3、db 選擇你要操作的數據庫名,這個可以稍后指定,非必須
4、port 端口號,默認值3306
5、charset 用來指定字符集(默認utf8)
二、操作數據庫
1、某些對象可以直接使用query(但是不推薦使用(所以這里基本略過),即使是使用也一定要先判斷是否存在這個屬性)
cxn.query('sql語句')
2、使用cur
cur=cxn.cursor()
這樣我們就能使用cur執行各種操作了,示例代碼如下:
1 import MySQLdb2 cxn=MySQLdb.Connect(host='localhost',user='root',passwd='',db='samp_db',port=3306)3 cur=cxn.cursor()4 result=cur.execute('select * from students')5 for i in cur.fetchall():6 PRint i這段代碼就能返回表students中的所有信息了,也就是你進入mysql輸入select * from students之后所顯示的內容。這里我就假設大家都了解sql的語句 使用了(不了解的可以去學或者用ORM操作數據庫)
接下來我們演示一下怎么更新表,以怎么向表中插入數據為例:
1 import MySQLdb 2 cxn=MySQLdb.Connect(host='localhost',user='root',passwd='',db='samp_db',port=3306) 3 cur=cxn.cursor() 4 def showtables(tname): 5 result=cur.execute('select * from %s'%tname) 6 for i in cur.fetchall(): 7 print i 8 showtables('students') 9 cur.execute("insert into students values(4,'liu4',100)")10 print 'after insert'11 showtables('students')
這樣我們會可以看待結果:
(1L, 'liu', Decimal('100'))(2L, 'liu2', Decimal('90'))(3L, 'liu3', Decimal('98'))after insert(1L, 'liu', Decimal('100'))(2L, 'liu2', Decimal('90'))(3L, 'liu3', Decimal('98'))(4L, 'liu4', Decimal('100'))
看似是一摸一樣的完成了這項工作,但是這時候我們用終端連上mysql。執行一條select * from students卻發現我們插入的那條并沒有進去。這是因為 我們還缺少一個commit工作。另外工作完成之后我們需要關閉與數據庫的連接,于是更改代碼如下
1 import MySQLdb 2 cxn=MySQLdb.Connect(host='localhost',user='root',passwd='',db='samp_db',port=3306) 3 cur=cxn.cursor() 4 def showtables(tname): 5 result=cur.execute('select * from %s'%tname) 6 for i in cur.fetchall(): 7 print i 8 showtables('students') 9 cur.execute("insert into students values(4,'liu4',100)")10 cxn.commit()11 print 'after insert'12 showtables('students')13 cur.close()14 cxn.close()三、獲取返回值
上面我們是將查詢的結果都存在了一個result變量里的,比呢切返回的都是tuple。但是cursor還有若干方法:來接收返回值
fetchall(self):接收全部的返回結果行.
fetchmany(self, size=None):接收size條返回結果行.如果size的值大于返回的結果行的數量,則會返回cursor.arraysize條數據.
fetchone(self):返回一條結果行.
scroll(self, value, mode='relative'):移動指針到某一行.如果mode='relative',則表示從當前所在行移動value條,如果mode='absolute',則表示從結果集的 第一行移動value條.
四、批量執行和參數
使用MySQLdb我們不僅可以執行一條語句,更可以將他與python結合起來,這樣我們就可以批量操做了
1 import MySQLdb 2 cxn=MySQLdb.Connect(host='localhost',user='root',passwd='',db='samp_db',port=3306) 3 cur=cxn.cursor() 4 def showtables(tname): 5 result=cur.execute('select * from %s'%tname) 6 for i in cur.fetchall(): 7 print i 8 showtables('students') 9 v=[]10 for i in range(10):11 v.append((i,'liu'+str(i),98))12 cur.executemany("insert into students values(%s,%s,%s)",v)13 cxn.commit()14 print 'after insert'15 showtables('students')16 cur.close()17 cxn.close()
上面用到了兩處參數,第五行和第12行
另外當執行多個命令要用executemany(op,args)它類似 execute() 和 map() 的結合, 為給定的每一個參數準備并執行一個數據庫查詢/命令
五、零碎
上面說過db這個屬性可以在連接之后指定,如下:
cxn.select_db('students')
新聞熱點
疑難解答