Python fdb firebirdsql.OperationalError: conversion error from string "?"

Question:

I use the following bit to make an sql statement using the key:values in the dict

record_number = 627
temp_dict = {
                "FOO": 752,
                "BAR": "test",
                "I": "zzzzz",
                "Hate": "tesname",
                "SQL": "testsomethingesle",
                "SO": "commentlol",
                "MCUH": "asadsa",
                "FILLING": "zzzzzz",
                "NAME": "''",
            }
update_query = (
                "UPDATE table_name SET {}".format(
                    ", ".join("{} = '?'".format(k) for k in temp_dict)
                )
                + " WHERE RECNUM = '"
                + record_number 
                + "';"
            )

update_values = tuple(temp_dict.values())
cur.execute(update_query, update_values)

the update_query prints out correctly

UPDATE table_name SET FOO = '?', BAR = '?', I = '?', Hate = '?', SQL = '?', SO = '?', MCUH = '?', FILLING = '?', NAME = '?' WHERE RECNUM = '627';

and the update_values also looks right

(752, 'test', 'zzzzz', 'tesname', 'testsomethingesle', 'commentlol', 'asadsa', 'zzzzzz', "''")

but I get back the following error
firebirdsql.OperationalError: conversion error from string "?"

My understanding is that ? is basically a placeholder value and if I put in a tuple or list as the second parameter in the cur.execute() it should replace the ? with the values passed in.
What am I doing wrong?

Asked By: P_stack_gprogram

||

Answers:

You’re generating a statement that has string literals with a question mark ('?'), not a question mark used as a parameter placeholder (plain ?). This means that when you execute the statement, you’re trying to assign the literal value ? to a column, and if that column is not a CHAR, VARCHAR or BLOB, this produces an error, because there is no valid conversion from the string ? to the other data types.

You need to uses "{} = ?" instead (notice the absence of single quotes around the question mark).

Answered By: Mark Rotteveel
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.