我有一个数据库表,某个 GUID(它是 en envoriment 变量的字符串)将被发送并插入到表中。该表确实包含多个列,但考虑到这些列可以在以后基于此唯一 ID 进行更新。但我就是无法插入表格。它说它的值必须是列表/元组/字典。我该如何解决这个问题?
deviceUID = str(socket.gethostname())
def deviceUID_check():
try:
sql_select_query = "Select * From kiosk_status where deviceUID = %s"
selectuidcursor = ourdb.cursor()
selectuidcursor.execute(sql_select_query, (deviceUID))
record = selectuidcursor.fetchall()
print("Found UID")
except mysql.connector.Error as error:
UIDinsertcursor = ourdb.cursor()
print("Failed to get record from MySQL table: {}".format(error))
sql = "INSERT INTO kiosk_status (deviceUID) VALUES %s"
val = (deviceUID)
UIDinsertcursor.execute(sql, val)
ourdb.commit()
print("Added a new deviceUid")
追溯:
Message=1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''DESKTOP-6H43O5H'' at line 1
Source=D:CLASSIFIED/Raspberry Pi Test\RaspBerry API\RaspBerry_API.py
StackTrace:
File "D:CLASSIFIED/Raspberry Pi Test\RaspBerry API\RaspBerry_API.py", line 52, in KIOSKUID_check
UIDinsertcursor.execute(sql, val)
File "D:CLASSIFIED/Raspberry Pi Test\RaspBerry API\RaspBerry_API.py", line 141, in <module> (Current frame)
deviceUID_check()
控制台日志:
pygame 2.1.2 (SDL 2.0.18, Python 3.7.8)
Hello from the pygame community. https://www.pygame.org/contribute.html
DESKTOP-6H43O5H
无法从 MySQL 表中获取记录:无法处理参数:str(DESKTOP-6H43O5H),它必须是 list、tuple 或 dict 类型
deviceUID = str(socket.gethostname())
新代码:
def deviceUID_check():
try:
sql_select_query = "Select * From kiosk_status where deviceUID = %s"
selectuidcursor = ourdb.cursor()
selectuidcursor.execute(sql_select_query, (deviceUID))
record = selectuidcursor.fetchall()
print("Found UID")
except mysql.connector.Error as error:
UIDinsertcursor = ourdb.cursor()
print("Failed to get record from MySQL table: {}".format(error))
sql = "INSERT INTO kiosk_status (deviceUID) VALUES %s"
val = (deviceUID,)
UIDinsertcursor.execute(sql, val)
ourdb.commit()
print("Added a new deviceUid")