这个SQL executemany命令出了什么问题?

2024-10-01 07:40:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我在python脚本中使用sqlunconnectionexecutemany命令,一次将成百上千行加载到我的表中。我执行此操作的代码如下:

try:
            items = []
            global jsontuple
            for tup in jsontuple:
                items.append(tup)
            global tweet_count
            stmnt = "INSERT INTO tbltweet" " (id,text,posted,username,retweetCount,link,friends,followers,partisanship)"" VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"
            sql_cursor.executemany(stmnt, items)
            sql_connection.commit()
            num_tweets = len(jsontuple)
            tweet_count += num_tweets
except:
            sql_connection.rollback()

全局jsontuple是一个元组的元组,其中每个组成元组都包含insert语句中引用的变量。由于executemany query需要数据采用列表类型的格式,所以我取jsontuple中的每个元组并将其附加到名为items的列表中。然后执行executemany查询。在

问题是,我几乎每次调用这个函数都会回滚(但是,有时函数执行时没有问题)。我的安排有什么问题?我已经进行了三次检查,以确保SQL表中的格式与我在脚本中进行的查询一致。我也尝试过只使用元组作为命令的数据,而不是将其转换为列表,但结果完全相同。在

谢谢!在


Tags: 命令脚本列表sqlcountitemsconnectionglobal