欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python連接mysql并提交mysql事務(wù)示例

 更新時(shí)間:2014年03月05日 09:20:14   作者:  
這篇文章主要介紹了python連接mysql并提交mysql事務(wù)的示例,需要的朋友可以參考下

復(fù)制代碼 代碼如下:

# -*- 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ì)代碼

    Python實(shí)現(xiàn)外星人去哪了小游戲詳細(xì)代碼

    今天為大家?guī)?lái)一款小游戲,名叫外星人去哪了,用Python語(yǔ)言實(shí)現(xiàn)完成,代碼簡(jiǎn)潔易懂,感興趣的小伙伴快來(lái)看看吧
    2022-03-03
  • Python學(xué)習(xí)筆記整理3之輸入輸出、python eval函數(shù)

    Python學(xué)習(xí)筆記整理3之輸入輸出、python eval函數(shù)

    這篇文章主要介紹了Python學(xué)習(xí)筆記整理3之輸入輸出、python eval函數(shù)的相關(guān)資料,需要的朋友可以參考下
    2015-12-12
  • Python入門之字典的使用教程

    Python入門之字典的使用教程

    Python字典是一種可變?nèi)萜髂P停铱纱鎯?chǔ)任意類型對(duì)象,如字符串、數(shù)字、元組等其他容器模型。本文將為大家詳細(xì)講講字典的使用教程,需要的可以參考一下
    2022-09-09
  • Python如何快速實(shí)現(xiàn)分布式任務(wù)

    Python如何快速實(shí)現(xiàn)分布式任務(wù)

    這篇文章主要介紹了Python如何快速實(shí)現(xiàn)分布式任務(wù),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-07-07
  • Python時(shí)間序列數(shù)據(jù)的預(yù)處理方法總結(jié)

    Python時(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-07
  • matplotlib之Pyplot模塊繪制三維散點(diǎn)圖使用顏色表示數(shù)值大小

    matplotlib之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遞歸實(shí)現(xiàn)打印多重列表代碼

    Python遞歸實(shí)現(xiàn)打印多重列表代碼

    今天小編就為大家分享一篇Python遞歸實(shí)現(xiàn)打印多重列表代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-02-02
  • 基于python介紹pytorch保存和恢復(fù)參數(shù)

    基于python介紹pytorch保存和恢復(fù)參數(shù)

    這篇文章主要介紹了基于python介紹pytorch保存和恢復(fù)參數(shù),為了恢復(fù)模型,我們需要用代碼生成框架,然后從磁盤加載參數(shù),下面具體的相關(guān)介紹,需要的小伙伴可以參考一下
    2022-03-03
  • python中GIL的原理及用法總結(jié)

    python中GIL的原理及用法總結(jié)

    在本篇文章里小編給大家整理的是一篇關(guān)于python中GIL的原理及用法總結(jié)內(nèi)容,有需要的朋友們可以學(xué)習(xí)參考下。
    2021-03-03
  • Python適配器模式代碼實(shí)現(xiàn)解析

    Python適配器模式代碼實(shí)現(xiàn)解析

    這篇文章主要介紹了Python適配器模式代碼實(shí)現(xiàn)解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08

最新評(píng)論