P
P
panamboor2020-12-02 15:55:26
Python
panamboor, 2020-12-02 15:55:26

Sqlite3 python (why is variable written with bracket and comma). Do I provide the code below?

def post_sql_query(sql_query):
    with sqlite3.connect('user_info.db') as connection:
        cursor = connection.cursor()
        try:
            cursor.execute(sql_query)
        except Error:
            pass
        result = cursor.fetchall()
        return result


def create_tables():
    users_query = '''CREATE TABLE IF NOT EXISTS USERS 
                        (user_id INTEGER PRIMARY KEY NOT NULL,
                        username TEXT,
                        reg_date TEXT,
                        balance INTEGER);'''
    post_sql_query(users_query)


def register_user(user, username,balance):
    user_check_query = f'SELECT * FROM USERS WHERE user_id = {user};'
    user_check_data = post_sql_query(user_check_query)
    if not user_check_data:
        insert_to_db_query = f'INSERT INTO USERS (user_id, username, reg_date, balance) VALUES ({user}, {username}, {ctime()},{balance});'
        print(insert_to_db_query)
        post_sql_query(insert_to_db_query)


#conn = sqlite3.connect('user_info.db')
#balance = conn.execute( f"select balance from USERS where user_id={message.from_user.id}").fetchone()
#print(balance) #outputs
( 1000,)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
ZIK1337, 2020-12-02
@panamboor

Because fetchone returns a tuple, which you store in a variable.
Change to

balance = conn.execute( f"select balance from USERS where user_id={message.from_user.id}").fetchone()[0]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question