D
D
dinozzzzawrik2022-03-29 17:52:50
Python
dinozzzzawrik, 2022-03-29 17:52:50

How to select SQLite3 cell correctly?

62431d01a4af1797619294.png

servers_db = sqlite3.connect('servers.db')
servers_sql = servers_db.cursor()

servers_sql.execute("""CREATE TABLE IF NOT EXISTS servers (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    guild INT,
    admin_channel INT
)""")


@bot.command()
async def admin_channel(ctx):
    channel = ctx.message.channel.id
    guild = int(ctx.message.guild.id)

    servers_sql.execute(f"SELECT admin_channel FROM servers WHERE guild = '{guild}' ")
    print(servers_sql.fetchone())
    if servers_sql.fetchone() is None:
        servers_sql.execute(f"INSERT WHERE guild = '{guild}' INTO servers(admin_channel) VALUES ('{channel}') ")

        servers_sql.execute('SELECT * FROM servers')
        rows = servers_sql.fetchall()
 
        for row in rows:
            print(row)
    else:
            #await channel.send('Бот уже был на этом сервере, ID добавлен')
        print("again")

    servers_db.commit()

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey Cheremisin, 2022-03-29
@dinozzzzaawrik

Start by getting acquainted with the SQL syntax and specifically the INSERT command, it can be useful :) - www.sql-tutorial.ru/ru/book_operator_insert.html

servers_sql.execute(f"INSERT INTO servers(admin_channel) VALUES ('{channel}')  WHERE guild = '{guild}' ")

V
Vindicar, 2022-03-29
@Vindicar

They wrote to you about INSERT, and I will add it. Never compose queries through string formatting, use parameter substitution .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question