B
B
b1gpups2021-07-30 17:41:27
Python
b1gpups, 2021-07-30 17:41:27

sqlite3.OperationalError: no such column: cash, how to solve?

i get sqlite3.OperationalError: no such column: cash error when i run the code

import sqlite3
import random


db = sqlite3.connect('dab.db')
sql = db.cursor()

db.commit()

sql.execute("""CREATE TABLE IF NOT EXISTS users (
    name TEXT,
    password TEXT,
    cash BIGINT
)""")


def reg():
    user_name = input("Enter name: ")
    user_password = input("Enter the password: ")

    sql.execute(f"SELECT name FROM users WHERE name = '{user_name}' ")
    if sql.fetchone() is None:
        sql.execute(f"INSERT INTO users VALUES (?,?,?)", (user_name,user_password,0))
        db.commit()

        print("Registred")
    else:
        print("This record exists")


def lag():
    global user_name
    user_name = input('Enter name: ')
    num = random.randint(1,2)

    for i in sql.execute(f'SELECT cash FROM users WHERE name = "{user_name}" '):
        balance = i[0]

    sql.execute(f"SELECT name FROM users WHERE name = '{user_name}' ")
    if sql.fetchone() is None:
        print('такого логина не существует. зарегистрируйтесь')
        reg()
    else:
        if num == 1:
            sql.execute(f"UPDATE users SET cash = {1000 + balance} WHERE name = '{user_name}' ")
            print('вы выйграли')
            db.commit()
        else:
            print('вы проиграли')
        

lag()

Answer the question

In order to leave comments, you need to log in

4 answer(s)
O
o5a, 2021-07-30
@o5a

So there is no such field in the table cash.
Apparently, the table was originally created with a different composition of fields, and it is already in the database (without cash), so CREATE IF NOT EXISTS is skipped.
Or delete the table from the database if the data is not needed: "DROP TABLE users"
Or add a field separately: "ALTER TABLE users ADD cash BIGINT".

S
Sergey Karbivnichy, 2021-07-30
@hottabxp

1) db - not a good name (may be misleading) 2) sql - not a good name (may be misleading) 3) no commit needed after connection 4)db = sqlite3.connect('dab.db')
5) global user_nameglobal should be used when you have sufficient Python programming experience and know what you are doing.
In 99% of cases, the exception is thrown due to F-strings. Therefore, replace all sql queries with the correct ones, and everything will work!

S
s4q, 2021-07-30
@s4q

What do you confirm on the fifth line? Opening a database?

sql.execute("""CREATE TABLE IF NOT EXISTS users (
name TEXT,
password TEXT,
cash BIGINT
)""")

db.commit()

V
Vindicar, 2021-07-30
@Vindicar

Does the table already exist in the database? Print the result of the query to your database:

SELECT sql FROM sqlite_master WHERE name = 'users';

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question