I need to work on a database with a telegram bot and I write this to manage the db file:
class DBfunction:
def __init__(self, dbname = 'example.db'):
self.debname = dbname
self.conn = sqlite3.connect(dbname)
self.cur = self.conn.cursor()
def search_db(self, telegram_id):
telegram_id = (telegram_id,)
sql = 'SELECT * FROM user WHERE id = ?;'
self.cur.execute(sql,telegram_id)
row = self.cur.fetchone()
self.conn.close()
return row
def newuser_db(self, tele_id, name, nick):
par = (tele_id, name, nick, 0)
#print(par)
sql = 'INSERT INTO user VALUES(?,?,?,?);'
self.cur.execute(sql, par)
self.conn.commit()
self.conn.close()
Is correct use close() in every def of the class or there is a better way?