aiosqlite "Result" 对象没有属性 "execute"

aiosqlite "Result" object has no attribue "execute"

我正在制作一个 discord.py 机器人,我目前在经济部分,但我遇到了一个我从未遇到过的 st运行ge 错误 运行进入之前和已经大部分python discord 帮助傻眼了。错误如下:

Command raised an exception: AttributeError: 'Result' has no attribute 'execute'

由于我是在游标对象而不是结果上执行的,所以我无法理解此错误的含义?

对于那些需要代码的人,给你:

# Imports
import discord
from discord.ext import commands
from random import randrange
import asyncio
import time
import aiosqlite

# Def balance funcs
def getBal(ctx, user : discord.Member):
  main = aiosqlite.connect('main.db')
  cursor = main.cursor()
  cursor.execute(f"SELECT balance FROM MainTable WHERE member_id = {user.id} AND guild_id = {ctx.guild.id}")
  result = cursor.fetchone()
  if result:
    return
  if not result:
    sql = "INSERT INTO MainTable(balance, guild_id, member_id, warns) VALUES(?,?,?,?)"
    val = (0, ctx.guild.id, user.id, 0)
  cursor.execute(sql, val)
  main.commit()
  cursor.close()
  main.close()

# Def main class
class Balance(commands.Cog):
    @commands.command(aliases=['bal'])
    async def balance(self, ctx, user : discord.Member = None):
      if user == None:
        user = ctx.author
        getBal(ctx, user)
      else:
        getBal(ctx, user)
      main = aiosqlite.connect('main.db')
      cursor = main.cursor()
      cursor.execute(f"SELECT balance FROM MainTable WHERE member_id = {user.id} AND guild_id = {ctx.guild.id}")
      result = cursor.fetchone()
      if result is not None:
        if user is None:
          embed = discord.Embed(title=f"**{ctx.author.mention}'s Balance**", description=f"**{ctx.author.mention}** has **{result[0]}** coins.", color=0xffffff)
          await ctx.send(embed=embed)
        else:
          embed = discord.Embed(title=f"**{user.mention}'s Balance**", description=f"**{user.mention}** has **{result[0]}** coins.", color=0xffffff)
          await ctx.send(embed=embed)
      else:
        await ctx.send("Critical Error! Please contact the developers.")
          
          

# Initialize
def setup(bot):
    bot.add_cog(Balance(bot))
    print('Balance is loaded')

您应该阅读 aiosqlite 的文档,因为它的工作方式与标准 sqlite3

不同

你需要

cursor = await main.execute(...)

编辑:

可能还需要在每个函数中使用await。并在 def

前加上 async
async def get_balance(ctx, user : discord.Member):   # PEP8: readable names
    
  main = await aiosqlite.connect('main.db')

  cursor = await main.execute(f"SELECT balance FROM MainTable WHERE member_id = {user.id} AND guild_id = {ctx.guild.id}")
  result = await cursor.fetchone()

  if result:
    return

  # there is no need to check `not result` 

  sql = "INSERT INTO MainTable(balance, guild_id, member_id, warns) VALUES(?,?,?,?)"
  val = (0, ctx.guild.id, user.id, 0)
  cursor = await main.execute(sql, val)

  await main.commit()
  await cursor.close()
  await main.close()

以后你必须 运行 它也与 await

await get_balance(ctx, user)

PEP 8 -- Style Guide for Python Code