無法處理參數:str() 它必須是列表、元組或字典類型
P粉585541766
P粉585541766 2024-01-02 09:02:53
0
1
502

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

全部回覆(1)
P粉447495069

如錯誤所述:

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

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

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

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

val = ("Providing Quality Tyre Services in Ponteland",)
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板