AJP
AJP

Reputation: 28463

Python SQLite delete rows IN

It's a similar problem to others out there but no body has clearly explained how to correct this:

process.py

import sqlite3  # version 2.6.0

connection = sqlite3.connect('./db.sqlite')
cursor = connection.cursor()
count_before = cursor.execute('SELECT COUNT(*) FROM r_data;').fetchall()[0][0]
print('{} rows in {}'.format(count_before, table_name))

query = 'DELETE FROM r_data WHERE pk IN (501, 668);'
print('Deleted ', cursor.execute(query).rowcount, 'rows')

count_after = cursor.execute('SELECT COUNT(*) FROM r_data;').fetchall()[0][0]
print('{} rows in {}'.format(count_after, table_name))
cursor.close()
connection.close()

output:

$ python process.py 
824 rows
822 rows
$ python process.py 
824 rows
822 rows

I know that the SQL queries are fine because I can run them successfully through other sqlite "clients" such as the firefox sqlite manager plugin.

Upvotes: 1

Views: 826

Answers (1)

AJP
AJP

Reputation: 28463

Right, need to commit on connection:

cursor.close()
connection.commit()   # <<<<< !!
connection.close()

Upvotes: 1

Related Questions