【发布时间】:2016-01-27 15:29:55
【问题描述】:
我的数据库无法查询。当我运行此代码时,它会显示一个错误。
def button_click(self):
# shost is a QString object
shost = self.le.text()
if shost:
s_h = "127.0.0.1"
s_n = "root"
s_p = ""
s_d = "code"
s_cn = mdb.connect(s_h, s_n, s_p, s_d)
cursor = s_cn.cursor()
today = datetime.date.today()
mac = get_mac()
query = "INSERT INTO `ac` (`acc`, `mac`, `date`) VALUES (%s, %s, %s)"
re = cursor.execute(query,(shost,mac,today,))
if re:
self.ex = Example()
self.ex.show()
else:
query1 = "SELECT * FROM `ac` WHERE `acc` = %s,`mac` = $s"
ck = cursor.execute(query1,(shost,mac))
if(ck):
self.ex = Example()
self.ex.show()
else:
print 'no'
我想知道如何用 python 变量编写 sql 代码,那么我该如何解决呢?
query = "INSERT INTO `ac` (`acc`, `mac`, `date`) VALUES (%s, %s, %s)"
re = cursor.execute(query,(shost,mac,today,))
和
query1 = "SELECT * FROM `ac` WHERE `acc` = %s,`mac` = $s"
ck = cursor.execute(query1,(shost,mac))
【问题讨论】:
标签: python mysql python-2.7 python-3.x