国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Python操作mysql數(shù)據(jù)庫(封裝基本的增刪改查)

 新學(xué)Python,在這里分享操作MySQL的全過程

1、安裝MySQL-python-1.2.3.win-amd64-py2.7.exe,這是操作mysql數(shù)據(jù)庫的python庫,有32位和64位之分,看自機器下載

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

3、引入mysql庫:

[python] view plain copy
  1. import MySQLdb  

4、獲取數(shù)據(jù)庫連接:

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

connect連接對象的方法:

close()  --關(guān)閉的方法

commit()   --如果支持事務(wù)則提交掛起的事務(wù)

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

cursor()  --返回連接的游標對象

5、獲取游標:

[python] view plain copy
  1. #該游標對象執(zhí)行查詢操作返回的結(jié)果是序列  
  2. cur=con.cursor()   
[python] view plain copy
  1. #該游標對象執(zhí)行查詢操作返回的結(jié)果是字典(字典可以方便我們隊查詢的結(jié)果進行操作,所以我采用這種方法)  
  2. cur=con.cursor(MySQLdb.cursors.DictCursor)    

游標對象的方法:

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

close()  --關(guān)閉游標

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

executemany(sql,params)--執(zhí)行單挑sql語句,但是重復(fù)執(zhí)行參數(shù)列表里的參數(shù),返回受影響的行數(shù)

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

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

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

nextset()  --條至下一行

setinputsizes(size)--定義cursor

游標對象的屬性:

description--結(jié)果列的描述,只讀

rowcount  --結(jié)果中的行數(shù),只讀

arraysize  --fetchmany返回的行數(shù),默認為1


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

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

附查詢結(jié)果:

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
最全總結(jié) | 聊聊 Python 數(shù)據(jù)處理全家桶(PgSQL篇)
Django 使用原生sql語句操作數(shù)據(jù)庫
MySQL基礎(chǔ)-存儲過程
使用Python操作postgresql數(shù)據(jù)庫
python操作mysql數(shù)據(jù)庫 | 菜鳥教程
Python連接MySQL數(shù)據(jù)庫方法介紹(超詳細!手把手項目案例操作)
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服