How to execute multi query in python?

自作多情 提交于 2019-12-02 04:15:45

问题


I want to execute the sql_query given in below function and doing this way :

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() function I am using to make a connection and it is defined separately .The query function i am using above is to execute any query passed to it . It is defined as :

def query(self,query,cursor):
    cursor.execute(query)
    self.close()
    return cursor 

It is not working properly . Is there anything I am missing ? Is there any mysqli function for python like that in php (multi_query) ?


回答1:


mysql-python can't execute semicolon separated multiple query statement. If you are looking for last_insert_id you can try with this:

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()


来源:https://stackoverflow.com/questions/20094456/how-to-execute-multi-query-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!