我想执行下面函数中给出的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
它无法正常工作。有什么我想念的吗?在php(multi_query)中有没有python的mysqli函数?
答案 0 :(得分:1)
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()