如何将列表保存到 mysql 数据库中?
How can i save a list into a mysql database?
我想将包含不和谐邀请的列表保存到我的数据库中。但是每次我 运行 这段代码,数据库中只保存了 1 个项目。我试图打印邀请者 ID 以检查他是否只运行一个项目,但他运行了每个项目的代码。那有什么问题呢?为什么 get 只保存了 1 项到数据库?
for invite in invites:
if invite.inviter is None:
continue
if not ctx.author.guild.get_member(int(invite.inviter.id)):
continue
if int(invite.uses) == 0:
continue
await cursor.execute("SELECT * FROM guild_invite_count WHERE guild_id = %s AND user_id = %s IS NOT NULL", (ctx.author.guild.id, invite.inviter.id))
find_user = await cursor.fetchone()
if find_user:
await cursor.execute("UPDATE guild_invite_count SET real_count = real_count + %s, total_count = total_count + %s WHERE guild_id = %s AND user_id = %s", (int(invite.uses), int(invite.uses), ctx.author.guild.id, invite.inviter.id))
else:
await cursor.execute("INSERT INTO guild_invite_count (guild_id, user_id, real_count, total_count) VALUES (%s, %s, %s, %s)", (ctx.author.guild.id, invite.inviter.id, int(invite.uses), int(invite.uses)))
print(invite.inviter.id)
await mydb.commit()
这个SQL
SELECT * FROM guild_invite_count WHERE guild_id = %s AND user_id = %s IS NOT NULL
逻辑上不正确。 AND user_id = %s IS NOT NULL
减少为 AND user_id = true
(除非参数为空)。就这样
SELECT * FROM guild_invite_count WHERE guild_id = %s AND user_id = %s
我想将包含不和谐邀请的列表保存到我的数据库中。但是每次我 运行 这段代码,数据库中只保存了 1 个项目。我试图打印邀请者 ID 以检查他是否只运行一个项目,但他运行了每个项目的代码。那有什么问题呢?为什么 get 只保存了 1 项到数据库?
for invite in invites:
if invite.inviter is None:
continue
if not ctx.author.guild.get_member(int(invite.inviter.id)):
continue
if int(invite.uses) == 0:
continue
await cursor.execute("SELECT * FROM guild_invite_count WHERE guild_id = %s AND user_id = %s IS NOT NULL", (ctx.author.guild.id, invite.inviter.id))
find_user = await cursor.fetchone()
if find_user:
await cursor.execute("UPDATE guild_invite_count SET real_count = real_count + %s, total_count = total_count + %s WHERE guild_id = %s AND user_id = %s", (int(invite.uses), int(invite.uses), ctx.author.guild.id, invite.inviter.id))
else:
await cursor.execute("INSERT INTO guild_invite_count (guild_id, user_id, real_count, total_count) VALUES (%s, %s, %s, %s)", (ctx.author.guild.id, invite.inviter.id, int(invite.uses), int(invite.uses)))
print(invite.inviter.id)
await mydb.commit()
这个SQL
SELECT * FROM guild_invite_count WHERE guild_id = %s AND user_id = %s IS NOT NULL
逻辑上不正确。 AND user_id = %s IS NOT NULL
减少为 AND user_id = true
(除非参数为空)。就这样
SELECT * FROM guild_invite_count WHERE guild_id = %s AND user_id = %s