新學(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庫:
4、獲取數(shù)據(jù)庫連接:
- 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、獲取游標:
- #該游標對象執(zhí)行查詢操作返回的結(jié)果是序列
- cur=con.cursor()
- #該游標對象執(zhí)行查詢操作返回的結(jié)果是字典(字典可以方便我們隊查詢的結(jié)果進行操作,所以我采用這種方法)
- 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、我自己封裝的一些基本操作
- # -*- coding: cp936 -*-
- import MySQLdb
-
- class MysqldbHelper:
- #獲取數(shù)據(jù)庫連接
- def getCon(self):
- try:
- conn=MySQLdb.connect(host='localhost',user='mjy',passwd='123',db='python',port=3306,charset='utf8')
- return conn
- except MySQLdb.Error,e:
- print "Mysqldb Error:%s" % e
- #查詢方法,使用con.cursor(MySQLdb.cursors.DictCursor),返回結(jié)果為字典
- def select(self,sql):
- try:
- con=self.getCon()
- print con
- cur=con.cursor(MySQLdb.cursors.DictCursor)
- count=cur.execute(sql)
- fc=cur.fetchall()
- return fc
- except MySQLdb.Error,e:
- print "Mysqldb Error:%s" % e
- finally:
- cur.close()
- con.close()
- #帶參數(shù)的更新方法,eg:sql='insert into pythontest values(%s,%s,%s,now()',params=(6,'C#','good book')
- def updateByParam(self,sql,params):
- try:
- con=self.getCon()
- cur=con.cursor()
- count=cur.execute(sql,params)
- con.commit()
- return count
- except MySQLdb.Error,e:
- con.rollback()
- print "Mysqldb Error:%s" % e
- finally:
- cur.close()
- con.close()
- #不帶參數(shù)的更新方法
- def update(self,sql):
- try:
- con=self.getCon()
- cur=con.cursor()
- count=cur.execute(sql)
- con.commit()
- return count
- except MySQLdb.Error,e:
- con.rollback()
- print "Mysqldb Error:%s" % e
- finally:
- cur.close()
- con.close()
-
- if __name__ == "__main__":
- db=MysqldbHelper()
- def get():
- sql="select * from pythontest"
- fc=db.select(sql)
- for row in fc:
- print row["ptime"]
-
- def ins():
- sql="insert into pythontest values(5,'數(shù)據(jù)結(jié)構(gòu)','this is a big book',now())"
- count=db.update(sql)
- print count
- def insparam():
- sql="insert into pythontest values(%s,%s,%s,now())"
- params=(6,'C#','good book')
- count=db.updateByParam(sql,params)
- print count
- def delop():
- sql="delete from pythontest where pid=4"
- count=db.update(sql)
- print "the:"+str(count)
- def change():
- sql="update pythontest set pcontent='c# is a good book' where pid=6"
- count=db.update(sql)
- print count
-
- #get()
- #ins()
- #insparam()
- #delop()
- #change()
-
-
-
-
-
附查詢結(jié)果:
本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請
點擊舉報。