Home:ALL Converter>how to insert variables into mysql query

how to insert variables into mysql query

Ask Time:2020-12-10T05:26:34         Author:COLDER

Json Formatter

hello i am new to python, i started learning how to work with mysql but the question arose - how to insert variables into mysql query?

I have code like this now:

table_name = "bot"
variable = 15

mySql_insert_query = """INSERT INTO %s (Id) 
                        VALUES
                        (%s) """    

I try to execute the code, but it gives an error: SyntaxError: EOF while scanning triple-quoted string literal attempt to call a nil value

How to write the code correctly? thanks

Author:COLDER,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/65225153/how-to-insert-variables-into-mysql-query
balderman :

Below is an example taken from here.\n...\ncursor = connection.cursor(prepared=True)\nsql_insert_query = """ INSERT INTO Employee\n (id, Name, Joining_date, salary) VALUES (%s,%s,%s,%s)"""\n\ninsert_tuple_1 = (1, "Json", "2019-03-23", 9000)\ncursor.execute(sql_insert_query, insert_tuple_1)\n...\n\nIn your case:\n cursor.execute("""INSERT INTO bot VALUES (%s)""", (15,))\n",
2020-12-09T21:33:14
yy