推荐学习书目
› 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 · 3955 views
    This topic created in 3798 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
    but0n
    Jun 11, 2016 via iPhone
    1
    一种是使用光标,一种不使用光标。
    有一些操作是光标的方法,有些则不用。
    dawncold
    dawncold
    Jun 11, 2016
    2
    @but0n 一般翻译的话游标比较常见吧
    Neveroldmilk
    Neveroldmilk
    Jun 12, 2016
    3
    在子集里使用 cursor ,可以提高操作效率,不过缺陷是需要本地缓存一部分数据。不使用 cursor 直接执行 SQL 命令则不会在本地缓存数据,但是全程依赖远程服务器,断网就完了。
    FreeBTC123
    FreeBTC123
    Jun 12, 2016❤️1
    4
    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   ·   Privacy   ·   Solana   ·   4555 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 09:34 · PVG 17:34 · LAX 01:34 · JFK 04:34
    ♥ Do have faith in what you're doing.