When accessing the class, the following error pops up: AttributeError: 'BotDB' object has no attribute 'cursor'
I don’t understand what the problem is.
import sqlite3
class BotDB:
def __init__(self, db_file):
self.conn = sqlite3.connect(db_file)
self.cursor - self.conn.cursor()
def user_exists(self, user_id):
result = self.cursor.execute("SELECT 'id' FROM 'users' WHERE 'user_id' = ?", (user_id,))
return bool(len(result.fetchall()))
def get_user_id(self, user_id):
result = self.cursor.execute("SELECT 'id' FROM 'users' WHERE 'user_id' = ?", (user_id,))
return result.fethcone()[0]
def add_user(self, user_id):
self.cursor.execute("INSERT INTO 'users' ('user_id') VALUES (?)", (user_id,))
return self.conn.commit()
def add_record(self, user_id, operation, value):
self.cursor.execute("INSERT INTO 'records' ('user_id', 'operation', 'value') VALUES (?, ?, ?)", (self.get_user_id(user_id), operation == '+', value))
return self.conn.commit()
def get_records(self, user_id, within = "*"):
if(within == 'day'):
result = self.cursor.execute("SELECT * FROM 'records' WHERE 'user_id' = ? AND 'date' BETWEEN datetime('now', 'start of day') AND datetime('now', 'localtime') ORDER BY 'date'",
self.get_user_id(user_id))
elif(within == 'month'):
result = self.cursor.execute(
"SELECT * FROM 'records' WHERE 'user_id' = ? AND 'date' BETWEEN datetime('now', '- 6 days') AND datetime('now', 'localtime') ORDER BY 'date'",
self.get_user_id(user_id))
elif (within == 'year'):
result = self.cursor.execute(
"SELECT * FROM 'records' WHERE 'user_id' = ? AND 'date' BETWEEN datetime('now', 'start of month') AND datetime('now', 'localtime') ORDER BY 'date'",
self.get_user_id(user_id))
else:
result = self.cursor.execute(
"SELECT * FROM 'records' WHERE 'user_id' = ? ORDER BY 'date'",
self.get_user_id(user_id))
return result.fetchall()
def close(self):
self.conn.close()
>Solution :
self.cursor - self.conn.cursor()
needs to be swapped to self.cursor = self.conn.cursor()
You are subtracting connection cursor from class cursor instead of setting the cursor value.