这不起作用,并显示错误“无法处理参数:str(在 Ponteland 提供优质轮胎服务),它必须是列表、元组或字典类型”
import mysql.connector from sentence_splitter import SentenceSplitter, split_text_into_sentences mydb = mysql.connector.connect( host="00.00.00.00", user="user", password="password", database="database" ) mycursor = mydb.cursor() sql = ("""SELECT blog_paragraph FROM blog_paragraph WHERE blog_topic like '%"%s"%'""") val = ("Providing Quality Tyre Services in Ponteland") mycursor.execute(sql,val) myresult = mycursor.fetchall() for x in myresult: print(x)
但是,当直接在查询中传递值时,它似乎运行时没有任何错误。
import mysql.connector from sentence_splitter import SentenceSplitter, split_text_into_sentences mydb = mysql.connector.connect( host="00.00.00.00", user="user", password="password", database="database" ) mycursor = mydb.cursor() sql = ("""SELECT blog_paragraph FROM blog_paragraph WHERE blog_topic like '%"Providing Quality Tyre Services in Ponteland"%'""") mycursor.execute(sql) myresult = mycursor.fetchall() for x in myresult: print(x)
尝试通过变量传递字符串参数,但似乎不起作用。
稍后找到答案
mycursor = mydb.cursor() val = "Providing Quality Tyre Services in Ponteland" sql = ("SELECT blog_paragraph FROM blog_paragraph WHERE blog_topic like '%%%s%%' " % val) mycursor.execute(sql) myresult = mycursor.fetchall()
如错误所述:
您的参数必须是预期类型之一,但您正在发送
str
您在
val
变量上添加了括号:但是
元组
是由逗号,
而不是括号定义的,这应该有效: