2016-07-15 44 views
1

刪除行這是一個類似的問題,別人在那裏,但沒有身體已明確闡述瞭如何糾正這一點,Python中的SQLite在

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() 

輸出:

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

我知道SQL查詢很好,因爲我可以成功地通過其他sqlite「cli ents「,例如firefox sqlite manager插件。

回答

1

權,需要提交的連接:

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