S
S
SiezurE2021-10-14 15:42:38
Python
SiezurE, 2021-10-14 15:42:38

How to fix error with database SQLite in Python?

import sqlite3

class Database:
def __init__(self, db_file):
self.connection = sqlite3.connect(db_file)
self.cursor = self.connection.cursor

def add_check(self, user_id, bill_id):
with self.connection:
self.cursor .execute("INSERT INTO 'check' ('user_id', 'bill_id') VALUES (?,?)", (user_id, bill_id,))

Tell me where, what I wrote wrong, gives an error:
'builtin_function_or_method' object has no attribute 'execute'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
retUrn3d, 2021-10-14
@SiezurE

Try this solution.

class DataBase:

    def __init__(self, db_file):
        self.conn = sqlite3.connect(db_file)
        self.cur = self.conn.cursor()

    # Аргумент arg принимает только кортежи(tuple), иначе ошибка.
    def add_check(self, arg: tuple):
        self.cur.execute("INSERT INTO check VALUES(?, ?);", arg)
        self.conn.commit()

# Создаем экземпляр класса, с указанием на БД.
db = DataBase('bot_base.db')

# Передаем методу класса add_check кортеж состоящий из user_id и bill_id
db.add_check((user_id, bill_id))

The values ​​you specify will be placed into the database in the order in which you place them in the tuple.
For example, in your database there is a table that has columns name and value , based on the example above, we pass a tuple where the first value is user_id , the second is bill_id .
The result will be this. name = user_id , value = bill_id .
I think I explained it very clearly and simply.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question