這不起作用,並顯示錯誤“無法處理參數: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
變數上新增了括號:但是
元組
是由逗號,
而不是括號定義的,這應該有效: