Home  >  Q&A  >  body text

Unable to handle argument: str() it must be of type list, tuple or dictionary

This does not work and displays the error "Cannot handle argument: str (Quality Tire Service in Ponteland), it must be of type list, tuple or dictionary"

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)

However, when passing the value directly in the query, it seems to run without any errors.

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)

Tried passing string parameters via variables, but that doesn't seem to work.

Find the answer later

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粉585541766291 days ago450

reply all(1)I'll reply

  • P粉447495069

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

    As the error states:

    Your parameter must be one of the expected types, but you are sending str

    You added brackets on the val variable:

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

    But the tuple is defined by commas , instead of brackets, this should work:

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

    reply
    0
  • Cancelreply