代码之家  ›  专栏  ›  技术社区  ›  sqram

Python MySQLdb更新查询失败

  •  14
  • sqram  · 技术社区  · 17 年前

    self.tbl.sql.use(self.tbl.database)       # switches to correct database. I've printed this and it uses the corrected db
    if self.tbl.sql.execute(query) == True:
        print sql_obj.rows_affected()         # returns 1 (since i only do 1 query)
    

    def execute(self, query):
    
        try:
            self.cursor.execute(query)
            return True
        except MySQLdb.ProgrammingError as error:
            print "---->SQL Error: %s" % error
            return False
        except MySQLdb.IntegrityError as e:
            print "--->SQL Error: %s" % e    
            return False
    

    2 回复  |  直到 12 年前
        1
  •  20
  •   Wim    16 年前

    我相信@Jason Creighton和@S.Lott是正确的。

    至少如果您要更新的表位于事务存储引擎上。 InnoDB ISAM

    commit() 在关闭连接对象之前,请先打开它,否则必须将连接设置为自动提交模式。我不知道你是如何为MySQLdb连接做到这一点的,我想你要么为连接构造函数设置一个参数,要么在创建连接对象后设置一个属性。

    conn = mysql.connection(host, port, autocommit=True)
    
    # or
    conn = mysql.connection(host, port)
    conn.autocommit(True)
    
        2
  •  16
  •   Jason C    17 年前

    只是猜测:也许Python中的代码正在事务中运行,并且事务可能需要显式提交?

    entry in the MySQLdb FAQ