Python Mysqldb Insert With Prepared Statements
I have a table with fields TABLE_PASTE( user_text longtext NOT NULL, number integer NOT NULL ) I am trying to insert a row in this table TABLE_PASTE from python using
Solution 1:
With MySQLdb
you want to use parameterized (prepared statements technically don't exist in python) statements similar to string formatting in Python 2.x.
cursor.execute("Update TABLE_PASTE set user_text = %s where number = %s",(text,value))
Note that even though your value
is an int
you still must use %s
to format it in your query.
You can find a good tutorial on MySQLdb
here.
Solution 2:
simply you can just use
cursor.execute("Update TABLE_PASTE set user_text = {} where number = {}" .format(text,value))
where format function will format your input accordingly whether its an int or a string!
Post a Comment for "Python Mysqldb Insert With Prepared Statements"