1. 程式人生 > >Python操作mysql資料庫(封裝基本的增刪改查)

Python操作mysql資料庫(封裝基本的增刪改查)

新學Python,在這裡分享操作MySQL的全過程

1、安裝MySQL-python-1.2.3.win-amd64-py2.7.exe,這是操作mysql資料庫的python庫,有32位和64位之分,看自機器下載

2、64位機器安裝MySQL-python-1.2.3.win-amd64-py2.7.exe出現 which was not found the regidtry,請點這裡

3、引入mysql庫:

  1. import MySQLdb  

4、獲取資料庫連線:

  1. conn=MySQLdb.connect(host='localhost',user='mjy',passwd=
    '123',db='python',port=3306,charset='utf8')  

connect連線物件的方法:

close()  --關閉的方法

commit()   --如果支援事務則提交掛起的事務

rollback()  --回滾掛起的事務

cursor()  --返回連線的遊標物件

5、獲取遊標:

  1. #該遊標物件執行查詢操作返回的結果是序列
  2. cur=con.cursor()   
  1. #該遊標物件執行查詢操作返回的結果是字典(字典可以方便我們隊查詢的結果進行操作,所以我採用這種方法)
  2. cur=con.cursor(MySQLdb.cursors.DictCursor)    

遊標物件的方法:

callproc(name,[params]) --用來執行儲存過程,接收的引數為儲存過程的名字和引數列表,返回受影響的行數

close()  --關閉遊標

execute(sql,[params])--執行sql語句,可以使用引數,(使用引數時,sql語句中用%s進行站位注值),返回受影響的行數

executemany(sql,params)--執行單挑sql語句,但是重複執行引數列表裡的引數,返回受影響的行數

fetchone()  --返回結果的下一行

fetchall()  --返回結果的 所有行

fetchmany(size)--返回size條記錄,如果size大於返回結果行的數量,則會返回cursor.arraysize條記錄

nextset()  --條至下一行

setinputsizes(size)--定義cursor

遊標物件的屬性:

description--結果列的描述,只讀

rowcount  --結果中的行數,只讀

arraysize  --fetchmany返回的行數,預設為1

6、我自己封裝的一些基本操作

  1. # -*- coding: cp936 -*-
  2. import MySQLdb  
  3. class MysqldbHelper:  
  4.     #獲取資料庫連線
  5.     def getCon(self):  
  6.         try:  
  7.             conn=MySQLdb.connect(host='localhost',user='mjy',passwd='123',db='python',port=3306,charset='utf8')  
  8.             return conn  
  9.         except MySQLdb.Error,e:  
  10.             print"Mysqldb Error:%s" % e  
  11.     #查詢方法,使用con.cursor(MySQLdb.cursors.DictCursor),返回結果為字典    
  12.     def select(self,sql):  
  13.         try:  
  14.             con=self.getCon()  
  15.             print con  
  16.             cur=con.cursor(MySQLdb.cursors.DictCursor)  
  17.             count=cur.execute(sql)  
  18.             fc=cur.fetchall()  
  19.             return fc  
  20.         except MySQLdb.Error,e:  
  21.             print"Mysqldb Error:%s" % e  
  22.         finally:  
  23.             cur.close()  
  24.             con.close()  
  25.     #帶引數的更新方法,eg:sql='insert into pythontest values(%s,%s,%s,now()',params=(6,'C#','good book')
  26.     def updateByParam(self,sql,params):  
  27.         try:  
  28.             con=self.getCon()  
  29.             cur=con.cursor()  
  30.             count=cur.execute(sql,params)  
  31.             con.commit()  
  32.             return count  
  33.         except MySQLdb.Error,e:  
  34.             con.rollback()  
  35.             print"Mysqldb Error:%s" % e  
  36.         finally:  
  37.             cur.close()  
  38.             con.close()  
  39.     #不帶引數的更新方法
  40.     def update(self,sql):  
  41.         try:  
  42.             con=self.getCon()  
  43.             cur=con.cursor()  
  44.             count=cur.execute(sql)  
  45.             con.commit()  
  46.             return count  
  47.         except MySQLdb.Error,e:  
  48.             con.rollback()  
  49.             print"Mysqldb Error:%s" % e  
  50.         finally:  
  51.             cur.close()  
  52.             con.close()  
  53. if __name__ == "__main__":  
  54.     db=MysqldbHelper()   
  55.     def get():   
  56.         sql="select * from pythontest"
  57.         fc=db.select(sql)  
  58.         for row in fc:  
  59.             print row["ptime"]  
  60.     def ins():  
  61.         sql="insert into pythontest values(5,'資料結構','this is a big book',now())"
  62.         count=db.update(sql)  
  63.         print count  
  64.     def insparam():  
  65.         sql="insert into pythontest values(%s,%s,%s,now())"
  66.         params=(6,'C#','good book')  
  67.         count=db.updateByParam(sql,params)  
  68.         print count  
  69.     def delop():  
  70.         sql="delete from pythontest where pid=4"
  71.         count=db.update(sql)  
  72.         print"the:"+str(count)  
  73.     def change():  
  74.         sql="update pythontest set pcontent='c# is a good book' where pid=6"
  75.         count=db.update(sql)  
  76.         print count  
  77.     #get()     
  78.     #ins()
  79.     #insparam()
  80.     #delop()
  81.     #change()

附查詢結果: