1

I am new to coding and databases, I can not get the query to work if I write it long hand but I have a lot to carry out and want it in a function but cannot get it to work, it returns a parameters error

import  mysql.connector

def connection_check_1(query, value):

    mydb = mysql.connector.connect(
        host="******",
        user="*****",
        passwd="*****",
        database="****"
    )

    mycursor = mydb.cursor()
    mycursor.execute(query, (value))
    myresult = mycursor.fetchall()
    mydb.close()
    return myresult

value = "sheep"
query = 'select inlicence from licence where animal = %s'
myresult = connection_check_1(query, value)
print(myresult)

Here is the SQL table I have

create table licence 
(
    animal varchar (20) primary key,
    inlicence int (1)
);

This is the error I get

Traceback (most recent call last):
File "*******************", line 20, in
myresult = connection_check_1(query, value)
File "********************", line 13, in connection_check_1
mycursor.execute(query, (value))
File "********************************************88", line 246, in execute
prepared = self._cnx.prepare_for_mysql(params)
File "/home/kev/PycharmProjects/test bed/venv/lib/python3.5/site-packages/mysql/connector/connection_cext.py", line 535, in prepare_for_mysql
raise ValueError("Could not process parameters")
ValueError: Could not process parameters

I have tried changing the way the query is written, changing it to fetchall().

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 2
    The posted question does not appear to include any attempt at all to solve the problem. StackOverflow expects you to try to solve your own problem first, as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific problem you're having in a Minimal, Complete, and Verifiable example. For more information, please see [How to Ask](https://stackoverflow.com/help/how-to-ask) and [take the tour](https://stackoverflow.com/tour). – Gonçalo Peres Dec 22 '18 at 12:17
  • 1
    Please report the exact error message you get, including all of any traceback. – BoarGules Dec 22 '18 at 12:31
  • have included the error message, I have tried to change the query to add a string instead of the place card. – Kevin john Hustings Dec 22 '18 at 12:39
  • yes sorry @Tony, I am very new to all this (coding and stack overflow), I appreciate your comment. – Kevin john Hustings Dec 22 '18 at 15:43
  • @KevinjohnHustings - No need to apologise, welcome to Stack Overflow. – Tony Dec 23 '18 at 13:39

1 Answers1

4

Wrapping a value with () doesn't turn it in to a tuple. You probably meant to add a comma there:

mycursor.execute(query, (value,))
# Creates a one-element tuple-^
Mureinik
  • 297,002
  • 52
  • 306
  • 350