我试图让数据库每秒检查一次表,看看XP值是否达到800,如果达到,则将rank值设置为“Elite Genin”并将XP值设置为0。
@bot.event
async def on_message(message):
guild = message.guild.id
table = "PEOPLE_" + str(guild)
try:
connection = mysql.connector.connect(
host="localhost",
port="3306",
user="root",
password="root",
database="naruto_game"
)
cursor = connection.cursor()
sql_event_query = """CREATE EVENT geninpromotion
ON SCHEDULE EVERY 1 Second
STARTS CURRENT_TIMESTAMP + INTERVAL 1 Second
ENDS CURRENT_TIMESTAMP + INTERVAL 24 Hours
DO UPDATE """ + table + """ SET rank = 'Elite Genin' where xp = 800 AND SET xp = 0"""
cursor.execute(sql_event_query)
except mysql.connector.Error as error:
print("Failed to find name: {}".format(error))
finally:
if connection.is_connected():
cursor.close()
connection.close()
print("MySQL connection has been closed.")
print("Event created.")
但是当我运行后发送消息时,出现此错误。
Failed to find name: 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 'Hours DO UPDATE " + table + " SET rank = 'Elite Genin' where xp = 800 AND SET xp' at line 2
我看到三个语法错误。
您想要的时间间隔关键字是
HOUR
,而不是HOURS
。有关时间间隔语法的详细信息,请阅读 https://dev.mysql.com/doc/refman/8.4/en/expressions.html#temporal-intervals。
命名列时要小心
rank
,因为这是 MySQL 中的保留关键字。请参阅https://stackoverflow.com/a/23446378/20860
如果要在
UPDATE
语句中设置两列,请将它们设置在 WHERE
子句之前。
固定的SQL语法应该如下:
CREATE EVENT geninpromotion
ON SCHEDULE EVERY 1 Second
STARTS CURRENT_TIMESTAMP + INTERVAL 1 SECOND
ENDS CURRENT_TIMESTAMP + INTERVAL 24 HOUR
DO UPDATE mytable SET `rank` = 'Elite Genin', xp = 0 WHERE xp >= 800
注意修复:
HOUR
关键字。rank
,以防止其被解释为关键字。UPDATE
语句中的分配。