搜尋

首頁  >  問答  >  主體

無法處理參數:str() 它必須是列表、元組或字典類型

這不起作用,並顯示錯誤“無法處理參數: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()

P粉585541766P粉585541766388 天前527

全部回覆(1)我來回復

  • P粉447495069

    P粉4474950692024-01-03 07:17:23

    如錯誤所述:

    您的參數必須是預期類型之一,但您正在發送 str

    您在 val 變數上新增了括號:

    val = ("Providing Quality Tyre Services in Ponteland")

    但是元組是由逗號,而不是括號定義的,這應該有效:

    val = ("Providing Quality Tyre Services in Ponteland",)

    回覆
    0
  • 取消回覆