python連接mysql并提交mysql事務(wù)示例
# -*- coding: utf-8 -*-
import sys
import MySQLdb
reload(sys)
sys.setdefaultencoding('utf-8')
class DB(object):
def __init__(self,host='127.0.0.1',port=3306,user='root',passwd='123',database=''):
self.__host=host
self.__port=port
self.__user=user
self.__passwd=passwd
self.__database=database
self.__open=False
print '__init__'
def __connect__(self):
if self.__open == False:
print 'connect db...'
self.__conn = MySQLdb.connect(host=self.__host , port=self.__port , user=self.__user , passwd=self.__passwd,charset='utf8')
self.__open = True
def __executeSql__(self,sql):
self.__connect__()
self.__executor = self.__conn.cursor(cursorclass = MySQLdb.cursors.DictCursor)
self.__executor.execute('use '+self.__database) #切換數(shù)據(jù)庫(kù)
return self.__executor.execute(sql)
def executeQueryForObject(self , sql):
self.__executeSql__(sql)
return self.__executor.fetchone()
'''
返回key=value 字典
'''
def executeQueryAll(self , sql):
self.__executeSql__(sql)
return self.__executor.fetchall()
def executeUpdate(self ,sql='' , isAutoCommit=False):
c = self.__executeSql__(sql)
if isAutoCommit == True:
self.commit() #提交事務(wù)
return c
'''
#提交事務(wù)
'''
def commit(self):
self.__conn.commit() #提交事務(wù)
'''
#關(guān)閉數(shù)據(jù)庫(kù),釋放資源
'''
def closeDB(self):
if not self.__conn is None:
print 'close db...'
self.__conn.commit() #提交事務(wù)
self.__conn.close()
def print_parameters(self):
print self.__user
print self.__passwd
print self.__host
print self.__port
'''
if __name__ == '__main__':
db=DB(database='tb2013')
#db.print_parameters()
#db.executeSql('select * from tb_user')
print db.executeQueryForObject('select count(*) as count from tb_user')
_rows = db.executeQueryAll('select userid,nick from tb_user limit 10');
print _rows
for row in _rows:
print row
print 'nick:%s' % str(row['nick'])
print db.executeUpdate(sql='update tb_user set nick=\'test\' where userid=95084397',isAutoCommit=True)
db.closeDB()
'''
相關(guān)文章
Python實(shí)現(xiàn)外星人去哪了小游戲詳細(xì)代碼
今天為大家?guī)?lái)一款小游戲,名叫外星人去哪了,用Python語(yǔ)言實(shí)現(xiàn)完成,代碼簡(jiǎn)潔易懂,感興趣的小伙伴快來(lái)看看吧2022-03-03Python學(xué)習(xí)筆記整理3之輸入輸出、python eval函數(shù)
這篇文章主要介紹了Python學(xué)習(xí)筆記整理3之輸入輸出、python eval函數(shù)的相關(guān)資料,需要的朋友可以參考下2015-12-12Python如何快速實(shí)現(xiàn)分布式任務(wù)
這篇文章主要介紹了Python如何快速實(shí)現(xiàn)分布式任務(wù),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07Python時(shí)間序列數(shù)據(jù)的預(yù)處理方法總結(jié)
這篇文章主要介紹了Python時(shí)間序列數(shù)據(jù)的預(yù)處理方法總結(jié),時(shí)間序列數(shù)據(jù)隨處可見(jiàn),要進(jìn)行時(shí)間序列分析,我們必須先對(duì)數(shù)據(jù)進(jìn)行預(yù)處理。時(shí)間序列預(yù)處理技術(shù)對(duì)數(shù)據(jù)建模的準(zhǔn)確性有重大影響2022-07-07matplotlib之Pyplot模塊繪制三維散點(diǎn)圖使用顏色表示數(shù)值大小
在撰寫論文時(shí)常常會(huì)用到matplotlib來(lái)繪制三維散點(diǎn)圖,下面這篇文章主要給大家介紹了關(guān)于matplotlib之Pyplot模塊繪制三維散點(diǎn)圖使用顏色表示數(shù)值大小的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08基于python介紹pytorch保存和恢復(fù)參數(shù)
這篇文章主要介紹了基于python介紹pytorch保存和恢復(fù)參數(shù),為了恢復(fù)模型,我們需要用代碼生成框架,然后從磁盤加載參數(shù),下面具體的相關(guān)介紹,需要的小伙伴可以參考一下2022-03-03