V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Jun 11, 2016 · 3451 views
    This topic created in 3610 days ago, the information mentioned may be changed or developed.
    官方文档例子是这样的
    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 replies    2016-06-12 12:27:57 +08:00
    but0n
        1
    but0n  
       Jun 11, 2016 via iPhone
    一种是使用光标,一种不使用光标。
    有一些操作是光标的方法,有些则不用。
    dawncold
        2
    dawncold  
       Jun 11, 2016
    @but0n 一般翻译的话游标比较常见吧
    Neveroldmilk
        3
    Neveroldmilk  
       Jun 12, 2016
    在子集里使用 cursor ,可以提高操作效率,不过缺陷是需要本地缓存一部分数据。不使用 cursor 直接执行 SQL 命令则不会在本地缓存数据,但是全程依赖远程服务器,断网就完了。
    FreeBTC123
        4
    FreeBTC123  
       Jun 12, 2016   ❤️ 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.
    ```
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2500 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 15:39 · PVG 23:39 · LAX 08:39 · JFK 11:39
    ♥ Do have faith in what you're doing.