我正在制作一个不和谐的机器人。我尝试使用 SQLite3 数据库来增加我的机器人的经济性。但是当我创建专栏时出现错误:cursor.execute(f"INSERT INTO users VALUES ('{member}', {member.id}, 30, 0, 0, 1, {guild.id})")sqlite3.OperationalError: near "s": syntax error而且,这是代码:@client.eventasync def on_ready(): cursor.execute("""CREATE TABLE IF NOT EXISTS users ( name TEXT, id INT, cash BIGINT, rep INT, xp INT, lvl INT, server_id INT )""") for guild in client.guilds: for member in guild.members: if cursor.execute(f"SELECT id FROM users WHERE id = {member.id}").fetchone() is None: cursor.execute(f"INSERT INTO users VALUES ('{member}', {member.id}, 30, 0, 0, 1, {guild.id})") else: pass connection.commit()
1 回答
慕工程0101907
TA贡献1887条经验 获得超5个赞
代码注入错误!member
考虑一下如果字符串化是会发生什么Foo's Bar
。你最终会执行
INSERT INTO users VALUES ('Foo's Bar... -- ^ Syntax error
我相信
cursor.execute(f"SELECT id FROM users WHERE id = {member.id}") cursor.execute(f"INSERT INTO users VALUES ('{member}', {member.id}, 30, 0, 0, 1, {guild.id})")
应该
cursor.execute("SELECT id FROM users WHERE id = ?", ( member.id, )) cursor.execute("INSERT INTO users VALUES (?, ?, 30, 0, 0, 1, ?)", ( str(member), member.id, guild.id ) )
添加回答
举报
0/150
提交
取消