0

Ok, I am trying to figure out how to make my variable passed to my method, which that is the easy part. My main problem that I am having is that I need that value of that variable in my method equal to the value in the WHERE Statement. I was told to use %s to equal the value being passed, but MariaDB doesn't like the syntax. Any help would be greatly appreciated.

def ERRORDISPLAY(ErrorTpye): 
    #value = ErrorType
    conn = connection.MySQLConnection(user = 'user', password = '123456',
                                     host = 'localhost', database= 'HomeConnect')
    cursor = conn.cursor()
    query = ("SELECT errNumber, description FROM Error_List WHERE errNumber = %s value %s")
    num = ErrorType
    cursor.execut(query,(num))
    for (description) in cursor:
        return print(num, description)

ERRORDISPLAY(1)
Ilja Everilä
  • 50,538
  • 7
  • 126
  • 127
Michael Schultz
  • 109
  • 1
  • 3
  • 8
  • `%s value %s`? Is that some MySQL specific thing, or a typo? The root problem (ignoring all the typing errors) is that you would pass the integer as is as the 2nd argument to `execute()`, which expects a sequence of arguments. In other words `(num)` should be `(num,)`, because it is the comma that makes it a tuple. – Ilja Everilä Feb 17 '18 at 21:42

1 Answers1

-1

I got it all figured out. I had to cast the integer to a string. for some reason the MariaDB for Pi W does not like certain syntax. So it should look like this:

def ERRORDISPLAY(ErrorTpye): 
    conn = connection.MySQLConnection(user = 'user', password = '123456',
                                 host = 'localhost', database= 'HomeConnect')
    cursor = conn.cursor()
    value = ErrorList
    query = ("SELECT errNumber, description FROM Error_List WHERE errNumber =" + str(value))

    cursor.execute(query, (value))

    for (description) in cursor:
        return print(num, description)

ERRORDISPLAY(1)
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Michael Schultz
  • 109
  • 1
  • 3
  • 8
  • No, don't do this. Don't string format or concatenate values to queries. Your data is not code, so don't treat it as such. You were probably on the right track trying to use the `%s` placeholder (not to be mixed with python's old style string formatting), though you'd have to check what placeholder syntax your DB-API driver uses. – Ilja Everilä Feb 17 '18 at 21:45