Python/MySQL错误1064,无法计算ou

2024-09-24 02:18:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直想找出是什么导致了这个错误。我相信是在对数据库的最后一次查询中。我已经在上面写了评论。在

在过去的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

Tags: toinididsinputexecutedbtime
1条回答
网友
1楼 · 发布于 2024-09-24 02:18:31

您的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,滥用保留字。事实上:^{} is a reserved word。在

相关问题 更多 >