python - 连接或游标上是否使用了提交?

标签 python python-3.x sqlite commit

名为“Practical Programming: 2nd Edition”的书有冲突的代码。这是我的代码的开头:

import sqlite3

con = sqlite3.connect('stackoverflow.db')    
cur = conn.cursor()

要提交,我会使用 con.commit()cur.commit() 还是在不同的时间使用它们?摘自本书:

con.commit() :

con commit

cur.commit() :

cur.commit()

Documentation shows con.commit() :

enter image description here

最佳答案

我采纳了 unutbu 的建议并自己尝试了。

示例代码:

import sqlite3

con = sqlite3.connect('db.db')
cur = con.cursor()

data = [('data', 3), ('data2', 69)]

cur.execute('CREATE TABLE Density(Name TEXT, Number INTEGER)')

for i in data:
    cur.execute('INSERT INTO Density VALUES (?, ?)', (i[0], i[1]))

cur.commit()

PyCharm 运行:

Traceback (most recent call last):
  File "/Users/User/Library/Preferences/PyCharmCE2018.1/scratches/scratch_2.py", line 13, in <module>
    cur.commit()
AttributeError: 'sqlite3.Cursor' object has no attribute 'commit'

教科书错误。 cur.commit() 不存在。

感谢 unutbu 和 s3n0

关于python - 连接或游标上是否使用了提交?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50429589/

相关文章:

用于获取受 DES/kerberos 保护的 URL 的 Python 脚本

Python - 在列表中搜索多个值并执行多个操作

java - 游标 'order by' SQLite

python - 为什么一个可以和自身相加的类不能求和? (a+a 有效,sum([a,a]) 失败)

python - 模块 'xml' 中无名称 'py'

python - 如何让QTabWidget看起来透明?

python - 如何让 sqlite 在共享主机服务器上运行?

c++ - Cython 映射 c++ 数据结构

Python Pandas 从变量创建 Dataframe 列

python - sqlite python - 从txt文件中读取记录到表中