我想执行下面函数中给出的 sql_query 并这样做:
def get_globalid(self):
cursor = self.connect()
sql_query = "REPLACE INTO globalid (stub) VALUES ('a'); SELECT LAST_INSERT_ID() as id"
cursor = self.query(sql_query, cursor)
for row in cursor:
actionid = row[0]
return actionid
connect() 函数我用来建立连接,它是单独定义的。我上面使用的查询函数是执行传递给它的任何查询。它的定义为:
def query(self,query,cursor):
cursor.execute(query)
self.close()
return cursor
它无法正常工作。我有什么遗漏的吗? python 是否有像 php (multi_query) 中那样的 mysqli 函数?
最佳答案
mysql-python 无法执行分号分隔的多个查询语句。
如果您正在寻找last_insert_id
,您可以尝试使用以下方法:
conmy = MySQLdb.connect(host, user, passwd, db)
cursor = conmy.cursor()
sql_query = "REPLACE INTO globalid (stub) VALUES ('a')"
cursor.execute(sql)
last_insert_id = conmy.insert_id()
conmy.close()
关于python - 如何在Python中执行多重查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20094456/