Myquery is inserted if i give the static value in fields.but throws exception if i give the variable name. My Working code
import MySQLdb
import datetime
db = MySQLdb.connect("localhost","root","pass","selvapractice" )
cursor = db.cursor()
sql = "INSERT INTO selva(name) \
VALUES ('Selva') "
try:
cursor.execute(sql)
db.commit()
except:
print "dffds"
db.rollback()
db.close()
My Exception Code
import MySQLdb
import datetime
db = MySQLdb.connect("localhost","root","pass","selvapractice" )
cursor = db.cursor()
a="surya"
sql = "INSERT INTO selva(name) \
VALUES (%s) " %(a)
try:
cursor.execute(sql)
db.commit()
except:
print "dffds"
db.rollback()
db.close()
It prints dfffds
How to give the variable name in query?Any help will be greatly appreciated!