Mörkö
Mörkö

Reputation: 221

Python sqlite3 delete not doing anything

I am a noob about sqlite (but somewhat experienced as Pythonista), but I am deeply confused why this (Python 2.7, DBPATH is the path to the database)...

import sqlite3

connection = sqlite3.connect(DBPATH)
cursor = connection.cursor()
query = "SELECT * from jobs"
cursor.execute(query)
print(cursor.fectchall())
query = "DELETE from jobs"
cursor.execute(query)

...Outputs the contents of the table (thus the name of the table is right) without altering it. Could someone point out the obvious?

Upvotes: 0

Views: 546

Answers (2)

Rolf of Saxony
Rolf of Saxony

Reputation: 22443

From the manual:

commit()

This method commits the current transaction. If you don’t call this method, anything you did since the last call to commit() is not visible from other database connections. If you wonder why you don’t see the data you’ve written to the database, please check you didn’t forget to call this method.

Instead, you can set the 'isolation level' when you connect, which automates the commits for you.

self.db = sqlite3.connect(self.db_name, isolation_level=None)

Upvotes: 3

dot.Py
dot.Py

Reputation: 5157

You have to commit() after every CUD operation you do in your database.

If you Create, Update or Delete, then commit().

Upvotes: 2

Related Questions