I've been trying for a while now and have look online and can't figure it out.
Variables are numbers
and animals
sql = ("INSERT INTO favourite (number, info) VALUES (numbers, animals )")
cursor.execute(*sql)
conn.comit()
I've been trying for a while now and have look online and can't figure it out.
Variables are numbers
and animals
sql = ("INSERT INTO favourite (number, info) VALUES (numbers, animals )")
cursor.execute(*sql)
conn.comit()
sql = ("INSERT INTO favourite (number, info) VALUES (%s, %s)", (numbers, animals))
for safety, always use escape, see http://dev.mysql.com/doc/refman/5.7/en/string-literals.html
Use:
sql=("INSERT INTO favourite (number, info) VALUES ({},{})".format(numbers,animals))
Its always good to use format as per future references. Check https://docs.python.org/release/3.1.5/library/stdtypes.html#old-string-formatting-operations
I think the following should work. Let me know how it works out for you.
sql=("INSERT INTO favourite (number, info) VALUES ({},{})".format(numbers,animals))
sql = "INSERT INTO favourite (number, info) VALUES (%s, %s)"
val = (numbers, animals)
cursor.execute(sql, val)
conn.commit()
This works I just tested. Also you mispelled commit idk if that was intentional..
incase you dont have the top connection part right here
db = mysql.connector.connect(
host="localhost",
user="root",
password="password",
database="database"
)
cursor = db.cursor()
Here is alternative solution which works for me
cursor = conn.cursor()
query ="INSERT INTO favourite (number, info) VALUES ('"+ variable1 +"','"+ variable2+"')"
cursor.execute(query)
conn.commit()