在cursor.execute(查询)mysql.connector Python中使用multi = True

时间:2016-04-03 09:30:31

标签: python mysql

我在python mysql.connector中有以下查询:

query="SELECT `AUTO_INCREMENT`FROM  INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'cl55-mm-px-db' AND   TABLE_NAME   = 'dj_Venues';"
cursor.execute(query)
autoV=str(cursor.fetchone())
autoV=autoV.replace('(','').replace(')','').replace(',','')
query="REPLACE INTO dj_Venues SET venueName = %s,venueAddress = %s,venueBands = %s,venueResident = %s,city=%s,country=%s;'"
variable=(venue,address,venueID,venueID1,city,country1)
cursor.execute(query,variable )          
query="SELECT `AUTO_INCREMENT`FROM  INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'cl55-mm-px-db' AND   TABLE_NAME   = 'dj_Events';"
cursor.execute(query)
autoE=str(cursor.fetchone())
autoE=autoE.replace('(','').replace(')','').replace(',','')
query="REPLACE INTO dj_Events SET eventName = %s,bandsintownID = %s,residentID=%s,date = %s,startTime=%s,sourceBands=%s,sourceResident=%s;'"
variable=(eventName,eventID,eventID1,date,start,source,source)
cursor.execute(query,variable )
query="REPLACE INTO dj_Master SET artistID = %s,eventID = %s,venueID = %s;'"
variable=(artistID,autoE,autoV)
cursor.execute(query,variable )

当我运行此脚本时,我收到以下错误:

    "Use multi=True when executing multiple statements")
mysql.connector.errors.InterfaceError: Use multi=True when executing multiple statements

我在每个multi=True添加了cursor.execute(),但查询不会执行,因此我的表格仍为空!

1 个答案:

答案 0 :(得分:0)

您的所有陈述最后都有;个分号。删除这些,在cursor.execute()中运行时不需要这些,并且它们是创建错误消息的那些:

query="SELECT `AUTO_INCREMENT`FROM  INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = 'cl55-mm-px-db' AND   TABLE_NAME   = 'dj_Venues'"
cursor.execute(query)

接下来,不要连续使用str();如果您希望连接的值使用str.join()

autoE = ''.join(cursor.fetchone())
如果你只有一个值,

或只是索引进入元组:

autoE = cursor.fetchone()[0]

或仅使用一个变量使用元组赋值:

autoE, = cursor.fetchone()

如果行中有多个结果,后者会抛出错误,这有助于在查询更改时查找错误。