0

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?

4
  • 1
    After the conn.execute() toss in a conn.commit() to commit your changes. Commented Jun 17, 2016 at 17:53
  • 1
    The naming is confusing. Defining the cursor as conn (connection) is not helpful for understanding. In which case I think @bernie 's answer is actually connection.commit() because you're using non-standard naming? Commented Jun 17, 2016 at 18:01
  • Sadly the code started as piece of copypasta and evolved from there. I will fix the naming. Thank you all. Edit: Changed the variable to cursor. Commented Jun 17, 2016 at 18:51
  • At least as far as I know (and I'm still learning...) conn = sqlite3.connect(something.db) and c = connection.cursor() Commented Jun 17, 2016 at 18:55

2 Answers 2

3

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)
Sign up to request clarification or add additional context in comments.

Comments

2

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

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

1 Comment

Sorry that I can't select two "right" answers. I think both are very good. Thank you too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.