Python's Archiver

為方便港臺同胞閱覽,Python中國特別推出簡繁體內容轉換功能

zkfarmer 发表于 2008-10-26 21:11

13.13.6.1 使用快捷方法

使用连接对象非标准的execute,executemany和executescript方法,你的代码可以被更简明地编写,因为你不必明确地创建(通常不必要)游标对象。相反,被隐含创建的游标对象,这些快捷方法返回该游标对象。这样,你可以执行SELECT语句并且在连接对象上仅用一个单一的调用直接地遍历它。
import sqlite3

persons = [
    ("Hugo", "Boss"),
    ("Calvin", "Klein")
    ]

con = sqlite3.connect(":memory:")

# Create the table
con.execute("create table person(firstname, lastname)")

# Fill the table
con.executemany("insert into person(firstname, lastname) values (?, ?)", persons)

# Print the table contents
for row in con.execute("select firstname, lastname from person"):
    print row

# Using a dummy WHERE clause to not let SQLite take the shortcut table deletes.使用一个伪WHERE子句,不让SQLite删除快捷表。
print "I just deleted", con.execute("delete from person where 1=1").rowcount, "rows"

下载文本(原文件名:sqlite3/shortcut_methods.py)。

出处[url=http://www.okpython.com]PYTHON中国[/url],关于翻译的任何事情请EMAIL给我[color=Red][email=zkfarmer@gmail.com]zkfarmer[/email][/color],更多文档在[url=http://www.zkfarmer.org]我的站点[/url],[color=SeaGreen]请参阅官方英文文档[/color]。

页: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.