Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I wrote a class to work with the database

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()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading