K
K
KerMan_KSP2021-07-23 14:46:35
Python
KerMan_KSP, 2021-07-23 14:46:35

What is wrong between the quotes: "sqlite3.OperationalError: near "̀": syntax error"? I am learning to write a bot, if necessary, but I still don’t understand what is there?

@client.event
async def on_ready():
    cursor.execute("""CREATE TABLE IF NOT EXISTS users(
    id INT,
    server_id INT,
    name TEXT,
    mention TEXT,
    cash BIGINT,
    coin BIGINT
  )""")


    for guild in client.guilds:#т.к. бот для одного сервера, то и цикл выводит один сервер
      for member in guild.members:#цикл, обрабатывающий список участников
        if cursor.execute(f"SELECT id FROM users where id={member.id}").fetchone() == None:#проверка, существует ли участник в БД
          cursor.execute(f"INSERT INTO users VALUES({member.id}, '{member}', '<@{member.id}>', '{guild.id}', 0, 0)")#вводит все данные об участнике в БД
      else:#если существует
        pass
    connection.commit()#применение изменений в БД
    print("Клиент успешно запущен на сервере")#сообщение о готовности
    print(guild.id)#вывод id сервера

Ignoring exception in on_ready
Traceback (most recent call last):
File "C:\Users\Kerman\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "C:\Users\Kerman\Desktop\Z-coin 0.0.2\Discord_Bot.py", line 43, in on_ready
cursor.execute(f"INSERT INTO users VALUES({member .id}, '{member}', '<@{member.id}>', '{guild.id}', 0, 0)")#enters all member data into the
sqlite3 database.OperationalError: near " ": syntax error

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vindicar, 2021-07-23
@KerMan_KSP

First, if you use the INSERT INTO table VALUES (...) syntax , then the list of values ​​must exactly match the list of columns. Your order is clearly not the same. Use the syntax INSERT INTO table (column, column, ...) VALUES (value, value, ...) , so it's easier to check the correctness of the query.
Second, never form a query string using string formatting! This is a good way to get SQL injection, Bobby Tables will confirm. =) Use placeholders (look for the line "Never do this -- insecure!", and read it next).

S
Sergey Gornostaev, 2021-07-23
@sergey-gornostaev

Every week I write that you should not form queries by concatenation, interpolation and string formatting. Use prepared statements and you will be happy.

T
Turkmen Time, 2021-07-23
@MrSel

I advise you to study SQLAlchemy or peewee

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question