gpt4 book ai didi

Python/MySQL - 错误 1064,无法弄清楚

转载 作者:行者123 更新时间:2023-11-29 21:33:55 24 4
gpt4 key购买 nike

我一直在尝试找出导致错误的原因。我相信它是在对数据库的最后一次查询中。我已经用评论标记了它。

这个错误在过去 30 分钟里一直让我头疼。

import MySQLdb
import time

# Create a database connection
db = MySQLdb.connect(host="******", user="******", passwd="*****", db="*****")
cur = db.cursor()

# Create a query to select all IDs
cur.execute("SELECT id FROM users")
clientArray = []

# Loop over all IDs returned from query,
# save all IDs in the clientArray
for row in cur.fetchall():
clientID = str(row[0])
clientArray.append(clientID)


clientIDInput = ""
while True:
# Check and wait for input
clientIDInput = raw_input("")
if clientIDInput in clientArray:
# Check to see whether user is already signed in to the device
cur.execute("SELECT fitnessStatus FROM users WHERE id=%s", (clientIDInput))
data = cur.fetchone()
if data[0] == False:
cur.execute("UPDATE users SET fitnessStatus='1' WHERE id=%s", (clientIDInput))
checkInTime = time.strftime('%Y-%m-%d %H:%M:%S')
checkOutID = raw_input("")
if checkOutID == clientIDInput:
cur.execute("UPDATE users SET fitnessStatus='0' WHERE id=%s", (clientIDInput))
checkOutTime = time.strftime('%Y-%m-%d %H:%M:%S')
print checkInTime
print checkOutTime

### I BELIEVE THIS IS THE CAUSE OF THE ERROR ###
cur.execute("INSERT INTO activities (id, machinename, checkin, checkout, clientid) VALUES (NULL, Cross Trainer #5, %s, %s, %s)", (checkInTime, checkOutTime, clientIDInput))
# Send checkInTime and checkOutTime to database

最佳答案

您的 INSERT 语句中存在语法错误。尝试将字符串“Cross Trainer #5”括在单引号中:

cur.execute("INSERT INTO activities (id, machinename, checkin, checkout, clientid) VALUES (NULL, 'Cross Trainer #5', %s, %s, %s)", (checkInTime, checkOutTime, clientIDInput))`

幸运的是,语句本身已经用双引号括起来",因此不需要进一步更改:)

错误 1064 有点误导。它表明,amongst others ,滥用保留字。确实:CROSS is a reserved word .

关于Python/MySQL - 错误 1064,无法弄清楚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35046092/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com