Python SQLite 删除 IN 行

标签 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“客户端”(例如 firefox sqlite 管理器插件)成功运行它们。

最佳答案

对,需要在连接上提交:

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

关于Python SQLite 删除 IN 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38394364/

相关文章:

python - 在 django 中,返回重定向没有给我所需的输出

python - 删除列表中数字的倍数 :

python - 使用 BeautifulSoup 根据日期进行抓取

python - 如何在 youtube API 中将我的视频上传到其他 channel

c++ - QML TableView + PostgreSQL 数据库报错

android - 更新数据库时出现 CursorIndexOutOfBoundsException

android - 在这种情况下,用户可以清除共享首选项但不能清除数据库

python - 如何确定某个库(例如 pypi.org 上的库)是否适合 Python 3

node.js - node-sqlite3 在 run 方法中绑定(bind)参数失败

java - 添加到数据库时出现空指针异常