1

我的功能有问题:

def dataf (p,k):
    try:
        connection = mysql.connector.connect(host='host',
                                             database='products',
                                             user='user',
                                             password='pwd')
        sql_select_Query = "select a from table where b LIKE %s AND c LIKE %s"
        cursor = connection.cursor()
        cursor.execute(sql_select_Query, ('%' + p+ '%',), ('%' + k + '%',))
        records = cursor.fetchall()
        return records
    except Error as e:
        print("Error reading data from MySQL table", e)
    finally:
        if (connection.is_connected()):
            connection.close()
            cursor.close()

当我仅使用第一个占位符执行此功能时,一切正常。使用第二个占位符,我得到 TypeError: NoneType

例如,使用第二个占位符,我想检查 ca 列中的值是否类似于 = 0,5 kg。当我编写没有第二个占位符的查询并直接插入值时,一切正常:

sql_select_Query = "select a from table where b LIKE %s AND c LIKE '0,5 kg'"

我究竟做错了什么?

4

1 回答 1

0

好的,我明白了:

sql_select_Query = "select a from table where b LIKE %s AND c LIKE %s"
        cursor = connection.cursor()
        cursor.execute(sql_select_Query, ('%' + p+ '%','%' + k + '%',))

我得到了结果。

于 2019-08-26T14:10:01.300 回答