Python sqlite3游标没有属性提交

Kam*_*224 11 python sqlite

当我运行此代码时:

path = '~/Scripts/wallpapers/single.png'
conn = sqlite3.connect('/Users/Heaven/Library/Application Support/Dock/desktoppicture.db')
cur = conn.cursor();
cur.execute("insert into data values ('" + path + "');")
cur.commit()
Run Code Online (Sandbox Code Playgroud)

我收到以下错误

AttributeError: 'sqlite3.Cursor' object has no attribute 'commit'
Run Code Online (Sandbox Code Playgroud)

我完全不知道为什么.

Cha*_*ews 17

commit()是其成员的方法sqlite3.Connection没有sqlite3.Cursor.这是在文档中.


小智 8

它的

conn.commit()
conn.close() //if you intend to close it afterwards
Run Code Online (Sandbox Code Playgroud)

说明:游标仅用于将指令传递给sqlite,而您必须提交或关闭已连接到数据库的实例.


Num*_*uis 8

从sir_charles804的答案(抱歉没有足够的点将其添加为注释)后面应该是:

conn.commit()
Run Code Online (Sandbox Code Playgroud)

代替

cur.commit()
Run Code Online (Sandbox Code Playgroud)