亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費(fèi)注冊(cè) 查看新帖 |

Chinaunix

  平臺(tái) 論壇 博客 文庫(kù)
最近訪問(wèn)板塊 發(fā)新帖
查看: 3979 | 回復(fù): 0
打印 上一主題 下一主題

django中使用cursor.execute更新數(shù)據(jù)庫(kù)問(wèn)題? [復(fù)制鏈接]

論壇徽章:
0
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報(bào)告]
發(fā)表于 2009-09-04 19:13 |只看該作者 |倒序?yàn)g覽

執(zhí)行數(shù)據(jù)庫(kù)刪除,使用了django的cursor.execute(sql)進(jìn)行刪除數(shù)據(jù),發(fā)現(xiàn)了奇怪問(wèn)題,有時(shí)候可以執(zhí)行成功,有的時(shí)候執(zhí)行不成功。
通過(guò)SQL的監(jiān)控看到SQL語(yǔ)句是正常的,就是沒(méi)有執(zhí)行commit
代碼應(yīng)該是沒(méi)問(wèn)題的:
Python代碼
from django.db import connection  
  • cursor = connection.cursor()  cursor.execute("delete from event where id=%s",[event_id])  
  • cursor.close()  
    想不通問(wèn)題出在什么地方,有的地方用類似的代碼就可以,最后還是改成了django自己的那套刪除方法:
    object.delete()
    自己也封裝了個(gè)數(shù)據(jù)庫(kù)操作的類。
    Python代碼
    class Database:  
  •           def get_connection(self):  
  •         connection = Connection(host="localhost",user="root",passwd="",use_unicode=True,charset="utf8")          connection.select_db(‘ppsea_main’)  
  •         return connection        
  •     def get_cursor(self):          return self.get_connection().cursor()  
  •           # 根據(jù)SQL取一條指定數(shù)據(jù)  
  •     def select_fetchone(self,sql):          cursor = self.get_cursor()  
  •         cursor.execute(sql)          print "select_fetchone sql: %s" %(sql)  
  •         object = cursor.fetchone()          desc = cursor.description  
  •                   if object:  
  •             print object              d = {}  
  •             i = 0              for item in desc:  
  •                 d[item[0]] = object                  i=i+1  
  •             print "one %s" %(d)              return d  
  •         else:              return object  
  •    
  •     # 根據(jù)SQL取的數(shù)據(jù)列表      def select_fetchall(self,sql):  
  •         cursor = self.get_cursor()          cursor.execute(sql)  
  •                   print "select_fetchall sql: %s" %(sql)  
  •                   items = cursor.fetchall()  
  •                   desc = cursor.description  
  •         li = []          if items:  
  •             for item in items:                                  d = {}  
  •                 i = 0                  for de in desc:  
  •                     d[de[0]] = item                      i=i+1  
  •                 li.append(d);              return li  
  •         else:              return li  
  •                
  •     # 執(zhí)行插入和更新      def execute(self,sql):  
  •         print "execute sql : %s" %(sql)          connection = self.get_connection()  
  •         cursor=connection.cursor()          cursor.execute(sql)  
  •         connection.commit()  
    通過(guò)自己的方法調(diào)用也沒(méi)問(wèn)題,自己的理解應(yīng)該是django中使用cursor.execute執(zhí)行后沒(méi)有commit。
    不知道是不是django的版本問(wèn)題,我用的是Django-1.0.2-final,遇到類似問(wèn)題的哥們可以一起討論討論。
    更新:
    經(jīng)過(guò)不斷嘗試,終于試出解決方法,在execute后加上cursor.execute("commit")
    完整代碼:
    Python代碼
    from django.db import connection   
  • cursor = connection.cursor()    cursor.execute("delete from event where id=%s",[event_id])   
  • cursor.execute("commit")   cursor.close()   
                   
                   
                   

    本文來(lái)自ChinaUnix博客,如果查看原文請(qǐng)點(diǎn):http://blog.chinaunix.net/u2/84280/showart_2045759.html
  • 您需要登錄后才可以回帖 登錄 | 注冊(cè)

    本版積分規(guī)則 發(fā)表回復(fù)

      

    北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號(hào)-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號(hào):11010802020122 niuxiaotong@pcpop.com 17352615567
    未成年舉報(bào)專區(qū)
    中國(guó)互聯(lián)網(wǎng)協(xié)會(huì)會(huì)員  聯(lián)系我們:huangweiwei@itpub.net
    感謝所有關(guān)心和支持過(guò)ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請(qǐng)注明原作者名及出處

    清除 Cookies - ChinaUnix - Archiver - WAP - TOP