MySQL 中的 Python 更新

标签 python mysql bluetooth

我一直在努力让这个非常简单的查询在 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表的更改。

感谢大家的帮助:)

Python mySQL Update, Working but not updating table

关于MySQL 中的 Python 更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33028177/

相关文章:

java - Android BLE 设备无法相互检测

python - 将环境变量传递给 GAE 实例

python - 使用 Numpy (np.linalg.svd) 进行奇异值分解

python - 使用 Pyramid、遍历和 PyMongo 更改模板

mysql - Mysql数据库中非varchar列的加密和解密

mysql - 添加内部联接和分组依据时的 SQL 查询时间差给出了错误的计算

ios - iOS 的 BLE 外设名称不正确

linux - BlueZ scotest 应用程序和 Linux 配置无法连接到 SCO 套接字

python - 如何根据元组中的值删除列表中的元组?

mysql,创建动态自定义别名