如何在python中执行多重查询? [英] How to execute multi query in python?

查看:137
本文介绍了如何在python中执行多重查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想执行以下函数中给出的sql_query并这样做:

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()函数是单独定义的.上面我使用的查询函数是执行传递给它的任何查询.定义为:

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 

它不能正常工作.我有什么想念的吗?像php(multi_query)一样,是否有适用于python的mysqli函数?

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

推荐答案

mysql-python无法执行以分号分隔的多个查询语句. 如果您要查找last_insert_id,可以尝试以下操作:

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

这篇关于如何在python中执行多重查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆