SQLAstro
SQLAstro

Reputation: 25

Deleting from a database a selection in listbox

I basically have 2 python scripts one is for frontend other is for backend. On Frontend I have this:

def delete_command():
    back.delete(selected_tuple[0])

So basically click on a entry in a listbox then on backend script it has the database call (sqllite3)

def delete(id):
    BASE_DIR = os.path.dirname(os.path.abspath(__file__))
    db_path = os.path.join(BASE_DIR, "AVDatabase.db")
    conn=sqlite3.connect(db_path)
    cur=conn.cursor()
    cur.execute('DELETE * FROM "Books"  where BookId=?',(BookId,))
    conn.commit()
    conn.close()

But i keep getting an error:

cur.execute('DELETE * FROM "Books"  where BookId=?',(BookId,))
NameError: name 'BookId' is not defined

The database already exists so its not dynamically created not sure why it considers the BookID column as not defined, BookID is a primary key integer on the main Books Table.

Upvotes: 0

Views: 143

Answers (2)

CryptoFool
CryptoFool

Reputation: 23099

This has nothing to do with what's in your database, or even with the SQL statement itself. You just haven't defined BookId. This code causes the same error:

class Cur:
    def execute(self, statement, values):
        pass

cur = Cur()
cur.execute('DELETE * FROM "Books"  where BookId=?',(BookId,))

Result:

Traceback (most recent call last):
  File "...", line 10, in <module>
    cur.execute('DELETE * FROM "Books"  where BookId=?',(BookId,))
NameError: name 'BookId' is not defined

Change BookId to id, as @Powertieke says.

Upvotes: 0

Powertieke
Powertieke

Reputation: 2408

You are not defining BookId.

The delete() method requires a parameter named id and you are not using it anywhere in the definition.

replace BookId with id here: cur.execute('DELETE * FROM "Books" where BookId=?',(BookId,)) to cur.execute('DELETE * FROM "Books" where BookId=?',(id,))

def delete(id):
    BASE_DIR = os.path.dirname(os.path.abspath(__file__))
    db_path = os.path.join(BASE_DIR, "AVDatabase.db")
    conn=sqlite3.connect(db_path)
    cur=conn.cursor()
    cur.execute('DELETE * FROM "Books"  where BookId=?',(id,))
    conn.commit()
    conn.close()

Upvotes: 1

Related Questions