Error in my code: "AttributeError: 'Database' object has no attribute 'insert'"

Viewed 1821

Here is my code ,and the error is in the last lines when I try to add info to the Database.

import sqlite3

class Database:
    def __init__(self, db):
        self.conn = sqlite3.connect(db)
        self.cur = self.conn.cursor()
        self.cur.execute('CREATE TABLE IF NOT EXISTS houses (id INTEGER PRIMARY KEY,location text, bedrooms text, price text)')
        self.conn.commit()

def fetch(self):
    self.cur.execute("SELECT * FROM parts")
    rows = self.cur.fetchall()
    return rows

def insert(self, location, bedrooms, price):
    self.cur.execute("INSERT INTO houses VALUES (NULL, ?, ?, ?)", (location, bedrooms, price))
    self.conn.commit()

def remove(self, id):
    self.cur.execute("DELETE FROM houses WHERE id = ?", (id,))
    self.conn.commit()

def __del__(self):
    self.conn.close()

db = Database('houses.db')
db.insert("a","2","750.000€")
db.insert("b","1","300.000€")
db.insert("c","4","650.000€")
db.insert("c","3","500.000€")
db.insert("a","3","825.000€")
db.insert("b","5","1.200.000€")
db.insert("b","3","775.000€")

The error says db.insert("a","2","750.000€") AttributeError: 'Database' object has no attribute 'insert'

1 Answers

Your fetch, insert, remove and del functions are not indented. Indent them to match the init function and you should be good to go.

Related