MySQL Python 两个占位符的问题
MySQL Python problem with two Placeholders
我的功能有问题:
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
对于第二个占位符,我想检查 c 列中的值是否类似于 = 0,5 kg。当我在没有第二个占位符的情况下编写查询并直接插入值时,一切正常:
sql_select_Query = "select a from table where b LIKE %s AND c LIKE '0,5 kg'"
我做错了什么?
好的,我知道了:
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 + '%',))
结果出来了
我的功能有问题:
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
对于第二个占位符,我想检查 c 列中的值是否类似于 = 0,5 kg。当我在没有第二个占位符的情况下编写查询并直接插入值时,一切正常:
sql_select_Query = "select a from table where b LIKE %s AND c LIKE '0,5 kg'"
我做错了什么?
好的,我知道了:
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 + '%',))
结果出来了