弱弱问一个 python 操作 sqlite 的问题

2016-06-11 18:48:47 +08:00
 cxz
官方文档例子是这样的
con = sqlite3.connect('test.db')
cur = con.cursor()
cur.execute("create table people (name_last, age)")

网上有些例子是这样的
con = sqlite3.connect('test.db')
con.execute("create table people (name_last, age)")

试了一下两种都可以用,这两者有什么区别吗?哪种是最佳实践?
2614 次点击
所在节点    Python
4 条回复
but0n
2016-06-11 21:46:34 +08:00
一种是使用光标,一种不使用光标。
有一些操作是光标的方法,有些则不用。
dawncold
2016-06-11 22:04:25 +08:00
@but0n 一般翻译的话游标比较常见吧
Neveroldmilk
2016-06-12 08:56:43 +08:00
在子集里使用 cursor ,可以提高操作效率,不过缺陷是需要本地缓存一部分数据。不使用 cursor 直接执行 SQL 命令则不会在本地缓存数据,但是全程依赖远程服务器,断网就完了。
FreeBTC123
2016-06-12 12:27:57 +08:00
https://docs.python.org/2/library/sqlite3.html#sqlite3.Connection.execute

```
execute(sql[, parameters])
This is a nonstandard shortcut that creates an intermediate cursor object by calling the cursor method, then calls the cursor ’ s execute method with the parameters given.
```

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/284946

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX