如果我在字段中給出靜態值,則插入Myquery,但如果我給出變量名稱,則會拋出異常。 我的工作代碼如何在mysql查詢中使用python傳遞變量名稱
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()
我的異常代碼
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()
它打印dfffds
如何查詢給變量的名字嗎?任何幫助將不勝感激!
一點都沒有。 – 2014-09-24 04:28:40
我檢查它打印dfffds除了區塊 – Madhesh 2014-09-24 04:33:30
http://halfcooked.com/presentations/osdc2006/python_databases.html – 2014-09-24 04:36:31