我一直在努力让这个非常简单的查询在 Python 中实际工作。我是一个完整的python新手,字符串的处理方式似乎与我习惯的方式有很大不同。
我正在尝试的查询...
cur = db.cursor()
cur.execute("SELECT bluetooth_Id FROM student_Data")
data = cur.fetchall()
for row in data :
bluetoothId=row[0]
result=bluetooth.lookup_name(bluetoothId,timeout=5)
print(result)
if(result != None):
cur.execute("UPDATE student_Data SET attendance = 1 WHERE bluetooth_Id= %s",(bluetoothId))
print("UPDATE student_Data SET attendance = 1 WHERE bluetooth_Id= %s",(bluetoothId))
问题似乎是我的实际 SQL 查询格式不正确。我知道这一点是因为最后一个打印语句返回了这个
('UPDATE student_Data SET attendance = 1 WHERE bluetooth_Id= %s', 'th:is:is:my:bt:id')
当然那个 id 实际上不是它返回的 id...我不想把它给你 :)
我正在按照示例进行操作,但没有取得任何进展。我的蓝牙已打开,程序看到了我的蓝牙 ID,它处理了我的 mysql 表中已有的 ID 列表,但它没有更新任何记录.
而且我确实检查过以确保我在 mysql 表中正确输入了我的 ID,所以这也不是问题所在!
更新
我能够使用这个创建正确的 MySQL 查询:
cur.execute("UPDATE student_Data SET attendance = 1 WHERE bluetooth_Id = '%s"%(bluetoothId)+"'")
它创造
UPDATE student_Data SET attendance = 1 WHERE bluetooth_Id = '11:11:11:11:11:11'
但 MySQL 表仍然没有正确更新。我将不得不调查原因...
最佳答案
解决方案是这样的:
cur.execute("UPDATE student_Data SET attendance = 1 WHERE bluetooth_Id = '%s"%(bluetoothId)+"'")
然后我不得不添加
db.commit()
执行后,为了真正提交对MySQL表的更改。
感谢大家的帮助:)
关于MySQL 中的 Python 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33028177/