Skip to content Skip to sidebar Skip to footer

How To Insert Variables Into Mysql Query

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'

Solution 1:

Below is an example taken from here.

...
cursor = connection.cursor(prepared=True)
sql_insert_query = """ INSERT INTO Employee
                   (id, Name, Joining_date, salary) VALUES (%s,%s,%s,%s)"""

insert_tuple_1 = (1, "Json", "2019-03-23", 9000)
cursor.execute(sql_insert_query, insert_tuple_1)
...

In your case:

   cursor.execute("""INSERT INTO bot VALUES (%s)""", (15,))

Solution 2:

Just use :mySql_insert_query = "INSERT INTO" +table_name+"VALUES " + variable

Post a Comment for "How To Insert Variables Into Mysql Query"