Python MySQL:cursor.excute(query,args)失败,而cursor.execute(query)传递了相同的查询

时间:2018-08-27 22:54:25

标签: python mysql python-3.x mysql-python

我有一个python函数,可以获取如下版本信息:

def get_version_info(build_id, table_name='build_info'):
    query = "SELECT `build_commit` FROM `" + table_name + "`  WHERE `build_id` = %s "
    args = (build_id)
    #query = "SELECT `build_commit` FROM `" + table_name + "`  WHERE `build_id` = " + build_id
    build_commit = None
    try:
        dbconfig = read_db_config()
        conn = MySQLConnection(**dbconfig)
        cursor = conn.cursor()
        cursor.execute(query, args)
        #cursor.execute(query)
        row = cursor.fetchone()
        if row is not None:
           build_commit = row[0]

无论如何,此函数始终返回None。 当我将其更改为使用如下所示的非参数化查询时。它总是返回正确的build_commit

def get_version_info(build_id, table_name='build_info'):
    query = "SELECT `build_commit` FROM `" + table_name + "`  WHERE `build_id` = " + build_id
    ...
    cursor.execute(query)

有什么东西,在cursor.execute的参数化查询中丢失了,这就是失败的原因。

0 个答案:

没有答案