Skip to content
Advertisement

aiosqlite “Result” object has no attribue “execute”

I’m making a discord.py bot and I’m currently on the economy part, but I’m encountering a strange error that I’ve never ran into before and has most of the python discord help dumbfounded. The error is as follows:

Command raised an exception: AttributeError: ‘Result’ has no attribute ‘execute’

I’m having trouble understanding the meaning of this error due to the fact that I’m executing it on a cursor object, and not a result?

For those who need the code, here you go:

# 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')

Advertisement

Answer

You should read documentation for aiosqlite because it works different then standard sqlite3

You need

cursor = await main.execute(...)

EDIT:

It may also need to use await in every function. And add async before def

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()

and later you have to run it also with await

await get_balance(ctx, user)

PEP 8 — Style Guide for Python Code

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement