0
votes
import discord
from discord.ext import commands

import sqlite3
from config import settings

client = commands.Bot(command_prefix = settings['PREFIX'])
client.remove_command('help')

connection = sqlite3.connect('server.db')
cursor = connection.cursor()


@client.event
async def on_ready():
    cursor.execute("""CREATE TABLE IF NOT EXISTS users (
        name TEXT,
        id INT,
        cash bigint,
        rep INT
        lvl 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}, 0, 0, 1)")
            else:
                pass

    connection.comit()
    print('Bot connected')


@client.event
async def on_member_join(member):
    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}, 0, 0, 1)")
        connection.comit()
    else:
        pass


@client.command(aluases = ['balance', 'cash'])
async def __balance(ctx, member: discord.Member = None):
    if member is None:
        await ctx.send(embed = discord.Embed(
                description = f"""Баланс пользователя **{ctx.author}** составляет **{"cursor.execute(SELECT cash FROM users WHERE id = {}".fromat(ctx.author.id).fetchone()[0]}**"""
        ))
    else:
        await ctx.send(embed = discord.Embed(
            description = f"""Баланс пользователя **{ctx.member}** составляет **{"cursor.execute(SELECT cash FROM users WHERE id = {}".fromat(ctx.member.id).fetchone()[0]}**"""
        ))
client.run(settings['TOKEN'])   

Error:

Ignoring exception in on_ready
Traceback (most recent call last):
  File "C:\Users\gnati\AppData\Local\Programs\Python\Python38-32\lib\site-packages\discord\client.py", line 343, in _run_event
    await coro(*args, **kwargs)
  File "bot.py", line 27, in on_ready
    cursor.execute(f"INSERT INTO users VALUES ('{member}', {member.id}, 0, 0, 1)")
sqlite3.OperationalError: table users has 4 columns but 5 values were supplied
2
Doesn't the last line in the error message clearly tell you what's wrong?Shiva
Please edit this question to include what you have done to fix it and research you have done.user14977106
The last line shows the error like @Shiva said.user14977106

2 Answers

0
votes

The error is caused when you create the table, you have forgotten a comma in your SQL query.

To fix it use this:

cursor.execute("""CREATE TABLE IF NOT EXISTS users (
    name TEXT,
    id INT,
    cash bigint,
    rep INT,
    lvl INT
)""")
0
votes
cursor.execute("""CREATE TABLE IF NOT EXISTS users (
    name TEXT,
    id INT,
    cash bigint,
    rep INT
    lvl INT
)""")

Since there is no comma between rep INT and lvl INT, the table has only 4 columns where you think it has 5.

What actually happens when this CREATE TABLE statement is executed is explained here: What happens if I forget comma in my create sqlite table for a column?