#πŸ”’ not able to save to database

58 messages Β· Page 1 of 1 (latest)

proud solstice
#
@bot.command()
@commands.has_role('Administrator')
async def addmoney(userid, amount, user):
    try:
        test = False
        cursorobj = db.cursor()
        read = cursorobj.fetchall()
        for i in read:
            if userid in i:
                currency = int(i[2])
                currency += amount
                test = True
        if not test:
            data_insert = """INSERT INTO currency (NAME, ID, CURRENCY) VALUES (?, ?, ?);"""
            data = (user, userid, amount)
            cursorobj.execute(data_insert, data)
            db.commit()
            return f'Success! <@{userid}> has earned {amount} (1)'
        data_insert = """Update currency set CURRENCY = ? where userid = ?"""
        data = (amount, userid)
        cursorobj.execute(data_insert, data)
        db.commit()
        return f'Success! <@{userid}> has earned {amount} (2)'
    except Exception as e:
        print(e)
paper anvilBOT
#

@proud solstice

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

gloomy saffron
#

oh btw

#

use an async version of sqlite

#

aiosqlite and asqlite are good picks

proud solstice
#

does that fix the error?

formal scaffold
#

except Exception as e:
print(e)
Don't ever do that. At least print the traceback and not just the exception. In this case though, I don't see why you don't just let the command crash.

gloomy saffron
gloomy saffron
proud solstice
formal scaffold
#

I don't really get what you're doing here with test and cursorobj.fetchall

gloomy saffron
proud solstice
formal scaffold
#

Actually, the more I look at this command the more confused I am.

  • does returning a string from a command even do anything? don't you need to explicitly send a message instead?
  • where's the Context parameter?
gloomy saffron
formal scaffold
#

what library is this?

gloomy saffron
gloomy saffron
proud solstice
proud solstice
gloomy saffron
gloomy saffron
proud solstice
gloomy saffron
#
  1. add the ctx parameter at the start
  2. use ctx.send instead of return
  3. use the cursor to actually retrieve the data
  4. no idea why you're using a for loop when you can just use the WHERE clause
  5. remove the try-except block
  6. no idea what test is supposed to do
proud solstice
#

this isnt the full code

gloomy saffron
proud solstice
gloomy saffron
#
@bot.command()```
this turns your function into a `Command` object
proud solstice
#

the bot output message works, its just that it doesnt save into the message

gloomy saffron
#

!code

paper anvilBOT
#
Formatting code on Discord

Here's how to format Python code on Discord:

```py
print('Hello world!')
```

These are backticks, not quotes. Check this out if you can't find the backtick key.

For long code samples, you can use our pastebin.

gloomy saffron
gloomy saffron
proud solstice
#

!paste

paper anvilBOT
#
Pasting large amounts of code

If your code is too long to fit in a codeblock in Discord, you can paste your code here:
https://paste.pythondiscord.com/

After pasting your code, save it by clicking the Paste! button in the bottom left, or by pressing CTRL + S. After doing that, you will be navigated to the new paste's page. Copy the URL and post it here so others can see it.

proud solstice
#

full code is a lil too long

#

wait how do I use the WHERE to check if a person exists?

gloomy saffron
#

the whole reason for commands is that you can run them like <prefix>[command]

proud solstice
#

yea, I was using a random tutorial to learn about the discord bot, first time using this and databases, wanted to use this as practice

#

alr lemme check it out

formal scaffold
gloomy saffron
proud solstice
formal scaffold
#

whatever you're searching for. user_id = ? or whatever

proud solstice
#

ohh

gloomy saffron
proud solstice
#

oh wait nvm

gloomy saffron
proud solstice
#

wait how do I check if a erson exists, then check 1 column and set it to a variable if they exist?

paper anvilBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.