X
X
XWR2021-08-14 10:12:27
Python
XWR, 2021-08-14 10:12:27

Error: sqlite3.OperationalError: no such column how to fix?

The code:

import sqlite3

connect = sqlite3.connect("lun_db.db")
cursor = connect.cursor()
cursor.execute("""CREATE TABLE IF NOT EXISTS lun_db(
    user_id INTEGER,
    ochki INTEGER,
    user_name TEXT
)
""")
connect.commit()

userid = 111
username = "ppppp"
cursor.execute(f"SELECT user_id FROM lun_db WHERE user_id = '{userid}'")
if cursor.fetchone() is None:
    cursor.execute("INSERT INTO lun_db VALUES(?, ?, ?);", (userid, username ,0))
    connect.commit()

 cursor.execute(f'UPDATE lun_db SET user_name = {username} WHERE user_id = "{userid}"')
 connect.commit()


Mistake:
File "C:\lun_bot\test_ckay_aiogram.py", line 157, in echo_message
    cursor.execute(f'UPDATE lun_db SET user_name = {username} WHERE user_id = "{userid}"')
sqlite3.OperationalError: no such column: star

Why does not it work?
There is a user_name column, but it doesn't work...
Help please

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Daniil Kotyashkin, 2021-08-14
@XWR

It turns out this is because your "username" in Insert is not wrapped in quotes, and adding a user is also not correct in your table "INTEGER, INTEGER, TEXT", and you enter it as "INTEGER, TEXT, INTEGER" Here is the code:

cursor.execute(f"SELECT user_id FROM lun_db WHERE user_id = '{userid}'")
if cursor.fetchone() is None:
    cursor.execute("INSERT INTO lun_db VALUES(?, ?, ?);", (userid,  0, f"{username}"))
    connect.commit()

UPDATE:
Same with UPDATE
cursor.execute('UPDATE lun_db SET user_name = ? WHERE user_id = ?', (f"{username}", userid))

S
soremix, 2021-08-14
@SoreMix

The user_name column is there, but it doesn't work

It is, but the program tells you there is no star column
Learn how to write SQL queries correctly, do not use this f-formatting
cursor.execute('UPDATE lun_db SET user_name = ? WHERE user_id = ?', (username, userid))

The first screen explains how to use variables
https://docs.python.org/3/library/sqlite3.html

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question