【发布时间】:2018-06-13 08:14:51
【问题描述】:
谁能给我解释一下:
import sqlite3
db = sqlite3.connect(':memory:')
db.execute('create table t1 (id integer primary key, val text)')
db.execute('create table t2 (id integer primary key, val text)')
c = db.cursor()
c.execute('insert into t1 values (?, ?)', (1, 'a'))
c.execute('insert into t2 values (?, ?)', (1, 'b'))
c.execute('insert into t1 values (?, ?)', (2, 'c'))
c.execute('insert into t2 values (?, ?)', (2, 'd'))
c.execute('''select t1.id, t1.val, t2.val
from t1
left join t2 using (id)
where t1.id is not null
union all
select t2.id, t1.val, t2.val
from t2
left join t1 using (id)
where t2.id is not null
and t1.id is null
''')
for row in c:
print(row[0])
if row[0] == 1:
c2 = db.cursor()
c2.execute('delete from t1 where id = ?', (row[0],))
如果我注释掉最后三行,输出是:
1
2
但是如果我取消最后三行的注释,输出是:
1
2
1
即。第一个游标已使用在第二个游标中执行的 DML 的结果进行了更新。
这是预期的行为吗?有什么办法可以预防吗?
我正在运行 Python 3.6.3(根据 Ubuntu 17.10),以防万一。
【问题讨论】:
标签: python sql python-3.x sqlite python-3.6