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ù)庫
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ù)庫,釋放資源
'''
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 中的 WSGI 接口和 WSGI 服務(wù)的運(yùn)行
這篇文章主要介紹了Python 中的 WSGI 接口和 WSGI 服務(wù)的相關(guān)資料,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-12-12
python 普通克里金(Kriging)法的實(shí)現(xiàn)
這篇文章主要介紹了python 普通克里金(Kriging)法的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
最新解決沒有NVSMI文件夾以及nvidia-smi‘?不是內(nèi)部或外部命令也不是可運(yùn)行的程序或批處理文件
這篇文章主要介紹了解決沒有NVSMI文件夾以及nvidia-smi‘?不是內(nèi)部或外部命令也不是可運(yùn)行的程序或批處理文件,本文通過兩種問題分析給大家分享解決方法,需要的朋友可以參考下2023-01-01
python sqlite3 判斷cursor的結(jié)果是否為空的案例
這篇文章主要介紹了python sqlite3 判斷cursor的結(jié)果是否為空的案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03
關(guān)于Python中定制類的比較運(yùn)算實(shí)例
今天小編就為大家分享一篇關(guān)于Python中定制類的比較運(yùn)算實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
Python命令行參數(shù)解析模塊getopt使用實(shí)例
這篇文章主要介紹了Python命令行參數(shù)解析模塊getopt使用實(shí)例,本文講解了使用語法格式、短選項(xiàng)參數(shù)實(shí)例、長選項(xiàng)參數(shù)實(shí)例等內(nèi)容,需要的朋友可以參考下2015-04-04
Python爬蟲 scrapy框架爬取某招聘網(wǎng)存入mongodb解析
這篇文章主要介紹了Python爬蟲 scrapy框架爬取某招聘網(wǎng)存入mongodb解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-07-07
python中用shutil.move移動文件或目錄的方法實(shí)例
在python操作中大家對os,shutil,sys,等通用庫一定不陌生,下面這篇文章主要給大家介紹了關(guān)于python中用shutil.move移動文件或目錄的相關(guān)資料,需要的朋友可以參考下2022-12-12

