V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
cxz
V2EX  ›  Python

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

  •  
  •   cxz · 2016-06-11 18:48:47 +08:00 · 2576 次点击
    这是一个创建于 2881 天前的主题,其中的信息可能已经有所发展或是发生改变。
    官方文档例子是这样的
    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)")

    试了一下两种都可以用,这两者有什么区别吗?哪种是最佳实践?
    4 条回复    2016-06-12 12:27:57 +08:00
    but0n
        1
    but0n  
       2016-06-11 21:46:34 +08:00 via iPhone
    一种是使用光标,一种不使用光标。
    有一些操作是光标的方法,有些则不用。
    dawncold
        2
    dawncold  
       2016-06-11 22:04:25 +08:00
    @but0n 一般翻译的话游标比较常见吧
    Neveroldmilk
        3
    Neveroldmilk  
       2016-06-12 08:56:43 +08:00
    在子集里使用 cursor ,可以提高操作效率,不过缺陷是需要本地缓存一部分数据。不使用 cursor 直接执行 SQL 命令则不会在本地缓存数据,但是全程依赖远程服务器,断网就完了。
    FreeBTC123
        4
    FreeBTC123  
       2016-06-12 12:27:57 +08:00   ❤️ 1
    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.
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2196 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 02:12 · PVG 10:12 · LAX 19:12 · JFK 22:12
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.