0

我怀疑这主要是一个mysql问题。

我最近继承了一些 python 代码(我不完全是 pythonista)。构建两个查询然后一次将它们发送到数据库的代码看起来非常简单

query_1 = "insert into %s SELECT Foo, Bar FROM %s where Type in ('AB','BC') group by Foo;" %(filteredTable, rawTable)
self.connect(self.credentials, query_1)

query_2 = 'insert into %s SELECT a.*, b.Bar  FROM %s a left join %s b on a.Foo  = b.Foo  ;' % (finaltable, filteredTable, otherTable)
self.connect(self.credentials, query_2)

def connect(self, credentials, query):
    connection = None
    server, user, password, database = credentials
    try:
        connection = MySQLdb.connect(server, user, password, database, connect_timeout=10)
        cursor = connection.cursor()
        cursor.execute(query)
        result = cursor.fetchall()
    except MySQLdb.Error, e:
        self.logger.exception('event_description="Error with mysql"')
        raise
    if connection:
        connection.close()
    return result

query_2 有效,query_1 失败,除非我添加 cursor.execute("COMMIT;")。

我可以在我的代码中轻松解决这个问题,但我对为什么会这样感到目瞪口呆。为什么一个没有提交就成功了,而另一个失败了?

4

0 回答 0