13.13 sqlite3 -- DB-API 2.0 SQLite数据库接口[Python参考库翻译]

前端之家收集整理的这篇文章主要介绍了13.13 sqlite3 -- DB-API 2.0 SQLite数据库接口[Python参考库翻译]前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

13.13 sqlite3 -- DB-API 2.0 interface for sqlite databases

2.5版本中新增。 @H_301_13@

sqlite是一个提供轻量级的基于磁盘的数据库C语言库,它不需要一个独立的服务进程,并且允许你使用一种非标准的sql查询语言来访问数据库。其他程序也可以使用sqlite来管理内部数据存储。你也可以使用sqlite来构建应用程序原型,然后把代码迁移到一个更大的数据库,比如Postgresql或者Oracle @H_301_13@

pysqliteGerhard Haring写的,提供一个与PEP 249描述的DB-API 2.0规格说明书兼容的sql接口。 @H_301_13@

要使用这个模块,你必须现创建一个代表数据库的连接对象(Connection object)。如下的数据将被存储在/tmp/example文件中: @H_301_13@

conn = sqlite2.connect('/tmp/example') @H_301_13@

你还可以提供一个特殊的名称":memory:"在内存中来创建一个数据库 @H_301_13@

一旦你建立了一个连接(Connection),你就可以创建一个游标对象(Cursor object),并且调用它的execute()方法来执行sql命令: @H_301_13@

@H_301_13@

c = conn.cursor() @H_301_13@

@H_301_13@

#Creat table @H_301_13@

c.execute('''creat table stocks @H_301_13@

(data text,trans text,symbol text,@H_301_13@

qty real,price real)''') @H_301_13@

@H_301_13@

#Insert a row of data @H_301_13@

c.execute('''insert into stocks @H_301_13@

values(' 2006-01-05 ','BUY','RHAT',100,35.14)''') @H_301_13@

@H_301_13@

通常你的sql操作会用到你的Python程序中的变量的值。你不应该使用Python的字符串操作符来集合你的查询,因为这样是不安全的;它使你的程序容易首道sql注入攻击。 @H_301_13@

替代方法是使用DB-API的参数交换。在你需要使用一个变量的时候,使用"?"作为一个占位符,然后提供一个包含变量的元组作为油表对象的execute()方法的第二个参数。(其他的数据库模块可能使用不同的占位符,比如"%s"或者":1"。)例如: @H_301_13@

@H_301_13@

#Never do this -- insecure! @H_301_13@

symbol = 'IBM' @H_301_13@

c.execute('''... where symbol = '%s' % symbol) @H_301_13@

@H_301_13@

#Do this instead @H_301_13@

t = (symbol,) @H_301_13@

c.execute('select * from stocks where symbol=?',t) @H_301_13@

@H_301_13@

#Larger example @H_301_13@

for t in ((' 2006-03-28 ','IBM',1000,45.00),@H_301_13@

(' 2006-04-05 ','MSOFT',72.00),@H_301_13@

(' 2006-04-06 ','SELL',500,53.00),@H_301_13@

): @H_301_13@

c.execute('insert into stocks values (?,?,?)',t) @H_301_13@

@H_301_13@

为了在执行一个select语句后返回数据,你既可以把游标作为一个迭代器来对待,调用游标对象的fetchone()方法来返回一个单一的匹配行,也可以调用fetchall()方法得到一个所有匹配行的列表。 @H_301_13@

下面这个例子使用了迭代器的方式: @H_301_13@

@H_301_13@

>>> c = conn.cursor() @H_301_13@

>>> c.execute('select * from stocks order by price') @H_301_13@

>>> for row in c: @H_301_13@

... print row @H_301_13@

... @H_301_13@

(u' 2006-01-05 ',u'BUY',u'RHAT',35.140000000000001) @H_301_13@

(u' 2006-03-28 ',u'IBM',45.0) @H_301_13@

(u' 2006-04-06 ',u'SELL',53.0) @H_301_13@

(u' 2006-04-05 ',u'MSOFT',72.0) @H_301_13@

>>> @H_301_13@

@H_301_13@

See Also: @H_301_13@

http://www.pysqlite.org @H_301_13@

pysqlite的网站。 @H_301_13@

http://www.sqlite.org @H_301_13@

sqlite的网站;相关文档描述了语法以及对其支持的可爱的sql的可用的数据类型。 @H_301_13@

PEP 249,Database API Specification 2.0 @H_301_13@

Marc-André Lemburg写的PEP @H_301_13@

@H_301_13@

13.13.1 模块函数和常量

PARSE_DECLTYPES @H_301_13@

这个常量是用来作为connect函数的参数detect_types的值使用的。 @H_301_13@

设置它使sqlite3模块解析返回的每一列的声明类型。他会解析出声明类型中的第一个词,i. e. 对于'integer primary key',将会解析出'integer'。然后对于这一列,他会在转换字典中查找并且使用对这种类型注册的转换函数。注意:转换器的名称是大小写敏感的! @H_301_13@

PARSE_COLNAMES @H_301_13@

这个常量是用来作为connect函数的参数detect_types的值使用的。 @H_301_13@

设置它使sqlite接口解析返回的每一列的列名字。他会在其中查找一个这样书写的字符串[mytype],然后将‘mytype’作为这一列的类型。他将尝试在转换字典中查找‘mytype’的入口,然后使用在字典中找到的转换函数来返回值。在cursor.description中找到的名字只是这个列的第一个字,i. e. 如果你在sql中使用这样的语句:'as "x [datetime]"',那么将会解析出直到列名字中第一个空格出现时的任何字符——这个列的名字只是一个简单的'x' @H_301_13@

connect(database[,timeout,isolation_level,detect_types,factory])

打开一个到sqlite数据库文件database的连接。你可以使用':memory:'来打开一个驻留在内存中而不是在磁盘上的数据库连接。 @H_301_13@

当一个数据库被多重连接时(when a database is accessed by multiple connections),并且有一个进程更改了数据库,这个sqlite数据库将会被锁住直到交易被执行。timeout参数指定这个连接在抛出一个异常之前等待的时间。默认的timeout参数是5.0(5秒钟) @H_301_13@

对于isolation_level参数,请查看在 13.13.2 中描述的连接对象(Connection objects)isolation_level属性 @H_301_13@

sqlite本身只支持TEXT,INTEGER,FLOAT,BLOBNULL类型。如果你希望使用其他类型,你需要添加自己的支持detect_types参数,以及使用定制的利用模块级的register_converter函数注册converters,可以让你很容易实现它。 @H_301_13@

detect_types默认为0i. e. off,没有类型检测),你可以设置其为PARSE_DECLTYPESPARSE_COLNAMES的任意组合来将类型检测打开。 @H_301_13@

sqlite3默认模块使用其Connection类来实现connection调用。然而,你可以编写Connection类的一个子类,然后将你的类提供给factory参数来使用你自己的类建立连接。 @H_301_13@

详细信息请参阅本手册的 13.13.4 章节。 @H_301_13@

@H_301_13@

register_converter(typename,callable)

注册一个转换器(callable)来将数据库中的字节串转换为一个Python类型。对于数据库中的所有typename类型的数据,这个转换器都将被调用Confer the parameter detect_types of the connect function for how the type detection works.注意:typename和你的查询中的类型名字的大小写必须匹配。 @H_301_13@

register_adapter(type,callable)

注册一个转换器来将Python中的type类型转换为sqlite支持的一种类型。转换器只callable接受一个单一的Python值作为参数,也必须返回这些类型中的一个值:int,long,float,str(UTF-8 encoded)unicode代码或字节流。 @H_301_13@

complete_statement(sql)

返回True如果字符串sql是一个或多个用分号结束的sql语句。它不能识别出错误,如果sql语句在语法上是正确的,仅仅是在语义上不完整,但是语句是以分号结束的。 @H_301_13@

者可以用来构建一个sqlite的解释器,就如下面的例子一样: @H_301_13@

#A minimal sqlite shell for experiments @H_301_13@

@H_301_13@

import sqlite3 @H_301_13@

@H_301_13@

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

con.isolation_level = None @H_301_13@

cur = con.cursor() @H_301_13@

@H_301_13@

buffer = "" @H_301_13@

print "Enter your sql commands to execute in sqlite3." @H_301_13@

print "Enter a blank line to exit." @H_301_13@

@H_301_13@

while True: @H_301_13@

line = raw_input() @H_301_13@

if line == "": @H_301_13@

break @H_301_13@

buffer += line @H_301_13@

if sqlite3.complete_statement(buffer): @H_301_13@

try: @H_301_13@

buffer = buffer.strip() @H_301_13@

cur.execute(buffer) @H_301_13@

@H_301_13@

if buffer.lstrip().upper().startswith("SELECT"): @H_301_13@

print cur.fetchall() @H_301_13@

except sqlite3.Error,e: @H_301_13@

print "An error occurred:",e.args[0] @H_301_13@

buffer = "" @H_301_13@

@H_301_13@

con.close() @H_301_13@

@H_301_13@

enable_callback_tracebacks(flag) @H_301_13@

默认情况下,在用户定义的functions,aggregates,converters,authorizer等中你不会得到任何跟踪信息。如果你想要调试它们,你就可以用True作为flag参数的值来调用这个函数。之后,你就可以在sys.stderr中得到从回调中返回的跟踪信息。使用False来再次关闭这个功能 @H_301_13@

13.13.2 连接对象(Connection Objects

一个连接实例具有以下属性方法 @H_301_13@

isolation_level

得到或设置当前的隔离级别(isolation level)。设置为None使用自动模式,或"DEFERRED","IMMEDIATE""EXLUSIVE"。更具体的说明请参考 13.13.5 章中的"Controlling Transactions" @H_301_13@

cursor([cursorClass])

cursor方法接受一个单一的cursorClass参数。如果这个参数被提供,则必须是一个扩展了sqlite3.Cursor类的定制cursor类。 @H_301_13@

execute(sql,[parameters])

这是一个非标准的,通过调用cursor方法创建一个中间cursor对象的快捷方式,然后使用给出的参数调用cursorexecute方法 @H_301_13@

executemany(sql,[parameters])

这是一个非标准的,通过调用cursor方法创建一个中间cursor对象的快捷方式,然后使用给出的参数调用cursorexecutemany方法 @H_301_13@

executescript(sql_script)

这是一个非标准的,通过调用cursor方法创建一个中间cursor对象的快捷方式,然后使用给出的参数调用cursorexecutescript方法 @H_301_13@

creat_function(name,num_params,func)

创建一个用户定义的函数,创建以后你可以在sql语句中通过name名字使用,num_params是这个函数接受的参数的数量func是一个作为sql函数Python调用器。 @H_301_13@

这个函数返回sqlite支持的任意类型:unicode,str,int,bufferNone @H_301_13@

Example: @H_301_13@

@H_301_13@

import sqlite3 @H_301_13@

import md5 @H_301_13@

def md5sum(t): @H_301_13@

return md5.md5(t).hexdigest() @H_301_13@

@H_301_13@

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

con.creat_function("md5",1,md5sum) @H_301_13@

cur = con.cursor() @H_301_13@

cur.execute("select mdt(?)",("foo",)) @H_301_13@

print cur.getchone()[0] @H_301_13@

@H_301_13@

creat_aggregate(name,aggregate_class)

创建一个用户定义的聚合函数 @H_301_13@

一个聚合类必须提供一个step方法,它接受参数的数量num_params,和一个finalize方法,它返回最终的聚合结果。 @H_301_13@

finalize方法可以返回sqlite支持的任何类型:unicode,bufferNone @H_301_13@

Example: @H_301_13@

@H_301_13@

import sqlite3 @H_301_13@

@H_301_13@

class MySum: @H_301_13@

def __init__(self): @H_301_13@

self.count = 0 @H_301_13@

@H_301_13@

def step(self,value): @H_301_13@

self.count += value @H_301_13@

@H_301_13@

def finalize(self): @H_301_13@

return self.count @H_301_13@

@H_301_13@

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

con.creat_aggregate("mysum",MySum) @H_301_13@

cur = con.cursor() @H_301_13@

cur.execute("create table test(i)") @H_301_13@

cru.execute("insert into test(i) values (1)") @H_301_13@

cru.execute("insert into test(i) values (2)") @H_301_13@

cur.execute("select mysum(i) from test") @H_301_13@

print cur.fetchone()[0] @H_301_13@

@H_301_13@

creat_collation(name,callable)

使用指定的namecallable创建一个校对。调用器会被传入两个字符串参数。它应该返回-1,如果第一个排序比第二个低,返回0,如果两个排序一致,返回1,如果第一个排序比第二个高。需要指出的是,这个操作控制着排序(ORDER BY in sql),因此,你的比较不会影响其他sql操作。 @H_301_13@

注意:调用器将会按照Python的字节码来接受参数,这些参数通常的编码是UTF-8 @H_301_13@

下面的例子显示了一个定制的对"the wrong way"进行排序的校对: @H_301_13@

@H_301_13@

import sqlite3 @H_301_13@

@H_301_13@

def collate_reverse(string1,string2): @H_301_13@

return -cmp(string1,string2) @H_301_13@

@H_301_13@

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

con.create_collation("reverse",collate_reverse) @H_301_13@

@H_301_13@

cur = con.cursor() @H_301_13@

cur.execute("creat table test(x)") @H_301_13@

cur.executemany("insert into test(x) values (?)",[("a",),("b",)]) @H_301_13@

cur.execute("select x from test order by x collate reverse") @H_301_13@

for row in cur: @H_301_13@

print row @H_301_13@

con.close() @H_301_13@

@H_301_13@

如果要删除一个校对,用None作为调用器来调用create_collation:@H_301_13@

con.create_collation("reverse",None) @H_301_13@

interrupt()

你可以从另一个线程中调用这个方法来中断一个连接上可能进行的任何查询操作。查询操作将会中断,查询调用者将会得到一个异常。 @H_301_13@

set_authorizer(authorizer_callback)

这个动作注册一个调用器。在每一次尝试访问数据库中的一个表中的一列时,这个调用器都将会被调用调用器应该返回sqlITE_OK,如果访问被允许,返回sqlITE_DENY,如果整个sql语句因为错误而应该被中断,返回sqlITE_IGNORE,如果这一列应该作为NULL值来对待。这些实例在sqlite3模块中可用。 @H_301_13@

调用器的第一个参数表明什么类型的操作要进行审定。第二个参数和第三个参数根据第一个参数,会使参数或者None。如果可用的话,第四个参数是数据库的名字("main","temp"等等)。第五个参数是对试图进行的访问负责的最内的(inner-most)触发器的名字或者视图,或者为None如果这个访问直接来自于sql代码 @H_301_13@

关于第一个参数的可能的取值,以及依赖于第一个参数的第二个和第三个参数的含义,请参阅sqlite的文档。所有的必须的常量在sqlite3模块中都是可用的。 @H_301_13@

row_factory

你可以将这个属性改变为一个调用器的名字,这个调用器接受一个游标和一行的元组,返回计算后的行。这样,你可以实现返回结果的更高级的方法,比如,返回一个任然可以通过名字访问列的对象。 @H_301_13@

Example @H_301_13@

@H_301_13@

import sqlite3 @H_301_13@

@H_301_13@

def dict_factory(cursor,row): @H_301_13@

d = {} @H_301_13@

for idx,col in enumerate(cursor.description): @H_301_13@

d[col[0]] = row[idx] @H_301_13@

return d @H_301_13@

@H_301_13@

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

con.row_factory = dict_factory @H_301_13@

cur = con.cursor() @H_301_13@

cur.execute("select 1 as a") @H_301_13@

print cur.fetchone()["a"] @H_301_13@

@H_301_13@

如果返回一个元组不能满足需要,而你想要对列的基于名字的访问,那么你可以考虑将row_factory设置为高度优化的sqlite3.Row类型。Row既提供基于索引的,也提供基于名字的对列的访问,却几乎不需要消耗额外的内存。它可能会比你自己定制的基于字典的访问甚至基于db_row解决方法还要好。 @H_301_13@

text_factory

使用这个属性你可以控制对于TEXT数据类型返回什么样的对象。默认情况下,这个属性被设置为unicode,对于TEXTsqlite3模块会返回Unicode对象。如果你希望返回字节串(bytestrings)来替代,你可以设置其为str @H_301_13@

你也可以将其设置为任何接受一个单一字节串参数并且返回最终对象的调用器。 @H_301_13@

参考下面的解释实例代码 @H_301_13@

@H_301_13@

import sqlite3 @H_301_13@

@H_301_13@

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

cur = con.cursor() @H_301_13@

@H_301_13@

#Create the table @H_301_13@

con.execute("create table person(lastname,firstname):) @H_301_13@

@H_301_13@

AUSTRIA = u"/xd6sterreich" @H_301_13@

@H_301_13@

# by default,rows are returned as Unicode @H_301_13@

cur.execute("select ?",( AUSTRIA ,)) @H_301_13@

row = cur.fetchone() @H_301_13@

assert type(row[0]) == str @H_301_13@

# the bytestrings will be encoded in UTF-8,unless you stored garbage in the @H_301_13@

# database ... @H_301_13@

assert row[0] == AUSTRIA.encode("uft-8") @H_301_13@

@H_301_13@

# we can also implement a custom text_factory ... @H_301_13@

# here we implement one that will ignore Unicode characters that cannot be @H_301_13@

# decoded form UTF-8 @H_301_13@

con.text_factory = lambda x: unicode(x,"utf-8","ignore") @H_301_13@

cur.execute("select ?",("this is latin1 and would normally create errors" + u"/xe4/xf6/xfc".encode("latin1"),)) @H_301_13@

row = cur.fetchone() @H_301_13@

assert type(row[0]) == unicode @H_301_13@

@H_301_13@

# pysqlite offers a builtin optimized text_factory that will return bytestring @H_301_13@

# objects,if the data is in ASCII only,and otherwise return unicode objects @H_301_13@

con.text_factory = sqlite3.OptimizedUnicode @H_301_13@

cur.execute("select ?",)) @H_301_13@

row = cur.fetchone() @H_301_13@

assert type(row[0]) == unicode @H_301_13@

@H_301_13@

cur.execute("select ?",(" germany ",)) @H_301_13@

row = cur.fetchone() @H_301_13@

assert type(row[0]) == str @H_301_13@

@H_301_13@

total_changes

返回从数据库连接开始后的所有被修改,插入或删除的行数。 @H_301_13@

13.13.3 游标对象(Cursor Objects)

一个游标实例拥有以下属性方法 @H_301_13@

execute(sql,[parameters])

执行一条sql语句。这条sql语句可能是参数化了(parametrized)(i. e. 用占位符替代了sql文字)sqlite3模块支持两种类型的占位符:问号(问好风格)和命名占位符(命名风格) @H_301_13@

这个例子展示了怎样使用问号风格的参数: @H_301_13@

@H_301_13@

import sqlite3 @H_301_13@

@H_301_13@

con = sqlite3.connect("mydb") @H_301_13@

@H_301_13@

cur = con.cursor() @H_301_13@

@H_301_13@

who = "Yeltsin" @H_301_13@

age = 72 @H_301_13@

@H_301_13@

cur.execute("select name_last,age from people where name_last=? and age=?",(who,age)) @H_301_13@

print cur.fetchone() @H_301_13@

@H_301_13@

下面的例子展示了如何使用命名风格: @H_301_13@

@H_301_13@

import sqlite3 @H_301_13@

@H_301_13@

con = sqlite3.connect("mydb") @H_301_13@

@H_301_13@

cur = con.cursor() @H_301_13@

@H_301_13@

who = "Yeltsin" @H_301_13@

age = 72 @H_301_13@

@H_301_13@

cur.execute("select name_last,age from people where name_last=:who and age=:age",{"who": who,"age": age}) @H_301_13@

print cur.fetchone() @H_301_13@

@H_301_13@

execute()方法执行一条单一sql语句。如果你试图用其执行多余一条的语句,将会抛出一个警告(Warning)。你可以是用executescript()来再一次调用中执行多条sql语句。 @H_301_13@

executemany(sql,seq_of_parameters)

执行一条sql语句利用所有参数序列,或者从参数中得到的遍历图(mappings)sqlite3模块也允许使用迭代器生成参数来替代序列。 @H_301_13@

@H_301_13@

import sqlite3 @H_301_13@

@H_301_13@

class IterChars: @H_301_13@

def __init__(self): @H_301_13@

self.count = ord('a') @H_301_13@

@H_301_13@

def __iter__(self): @H_301_13@

return self @H_301_13@

@H_301_13@

def next(self): @H_301_13@

if self.count > ord('z'): @H_301_13@

raise StopIteration @H_301_13@

self.count += 1 @H_301_13@

return (chr(self.count - 1),) # this is a 1-tuple @H_301_13@

@H_301_13@

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

cur = con.cursor() @H_301_13@

cur.execute("create table characters(c)") @H_301_13@

@H_301_13@

theIter = IterChars() @H_301_13@

cur.executemany("insert into characters(c) values (?)",theIter) @H_301_13@

@H_301_13@

cur.execute("select c from characters") @H_301_13@

print cur.fetchall() @H_301_13@

@H_301_13@

下面是一个使用生成器的小例子: @H_301_13@

@H_301_13@

import sqlite3 @H_301_13@

@H_301_13@

def char_generator(): @H_301_13@

import string @H_301_13@

for c in string.letters[:26]: @H_301_13@

yield (c,) @H_301_13@

@H_301_13@

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

cur = con.cursor() @H_301_13@

cur.execute("create table charaters(c)") @H_301_13@

@H_301_13@

cur.executemany("insert into characters(c) values (?)",char_generator()) @H_301_13@

@H_301_13@

cur.execute("select c from characters") @H_301_13@

print cur.fetchall() @H_301_13@

@H_301_13@

executescript(sql_script)

这是一个非标准的一次执行多条sql语句的简便方法。它首先提交一个COMMIT语句,然后将接收到的sql语句作为参数。 @H_301_13@

sql_script可以是一个字节串,也可以是一个Unicode字符串。 @H_301_13@

Example @H_301_13@

@H_301_13@

import sqlite3 @H_301_13@

@H_301_13@

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

cur = con.cursor() @H_301_13@

cur.executescript(""" @H_301_13@

create table person( @H_301_13@

firstname,@H_301_13@

lastname,@H_301_13@

age @H_301_13@

); @H_301_13@

@H_301_13@

create table book( @H_301_13@

title,@H_301_13@

author,@H_301_13@

published @H_301_13@

); @H_301_13@

@H_301_13@

insert into book(title,author,published) @H_301_13@

values ( @H_301_13@

'Dirk Gently''s Holistic Detective Agency',@H_301_13@

'Douglas Adams',@H_301_13@

1987 @H_301_13@

); @H_301_13@

""") @H_301_13@

@H_301_13@

rowcount

虽然sqlite3模块的游标类提供这一个属性,但是数据库引擎本身对"rows affected"/"rows selected"的定义的支持要更快一些。 @H_301_13@

对于select语句,rowcount总是None,以为在所有行都被取出之前,我们无从得知一个查询所产生的行数。 @H_301_13@

对于delete语句,如果你使用DELETE FROM而没有任何条件的话,sqliterowcount作为0报告。 @H_301_13@

根据Python DB API Spec的要求,这个rowcount属性“是-1,如果在这个游标上还没有executeXX()被执行过,或者其最后一册操作的rowcount对于接口不可知。” @H_301_13@

13.13.4 sqlite and Python types

@H_301_13@

13.13.4 .1 Introduction

sqlite本身支持以下类型:NULL,REAL,TEXT,BLOB. @H_301_13@

从而,如下的Python类型可以直接存入sqlite,而不会出现任何问题: @H_301_13@

Python type @H_301_13@

sqlite type @H_301_13@

None @H_301_13@

NULL @H_301_13@

int @H_301_13@

INTEGER @H_301_13@

long @H_301_13@

INTEGER @H_301_13@

float @H_301_13@

REAL @H_301_13@

str(UTF8-encoded) @H_301_13@

TEXT @H_301_13@

unicod @H_301_13@

TEXT @H_301_13@

buffer @H_301_13@

BLOB @H_301_13@

下面是默认情况下sqlite类型转换为Python类型的对照: @H_301_13@

sqlite type @H_301_13@

Python type @H_301_13@

NULL @H_301_13@

None @H_301_13@

INTEGER @H_301_13@

intlong,取决于其大小 @H_301_13@

REAL @H_301_13@

float @H_301_13@

BLOB @H_301_13@

buffer @H_301_13@

sqlite3模块的类型系统可以通过两种方式扩展:可以通过对象适配器(object adaptation)将补充的Python类型存储在一个sqlite数据库中,也可以让sqlite3模块通过转换器把sqlite类型转换为不同的Python类型。 @H_301_13@

13.13.4 .2.2 使用适配器在sqlite数据库中储存补充的Python类型

如前所述,sqlite本身只支持有限的几种类型。为了在sqlite中使用其他Python类型,你必须使它们适配sqlite3模块支持的类型:None,unicodebuffer之一。 @H_301_13@

sqlite3模块使用Python对象适配器,在PEP 246中对此有描述。使用的约定为PrepareProtocol @H_301_13@

有两种方法能够使sqlite3模块将定制的Python类型适配为被支持的类型。 @H_301_13@

13.13.4 .2.1 让你的对象自适应

如果你自己编写对象的话,这是一个很好的方法。假设你有这样的一个类: @H_301_13@

class Point(object): @H_301_13@

def __init__(self,x,y): @H_301_13@

self.x,self.y = x,y @H_301_13@

现在你希望将point储存于sqlite中的一个单独的列中。首先你需要选择一个支持的类型来替代point。让我们只是用一个字符串,用分号分割两个坐标。然后你需要为你的类添加一个__conform__(self,protocol)方法,它必须返回转换后的值。参数protocol将会是PrepaerProtocol @H_301_13@

@H_301_13@

import sqlite3 @H_301_13@

@H_301_13@

class Point(object): @H_301_13@

def __init__(self,y @H_301_13@

@H_301_13@

def __conform__(self,protocol): @H_301_13@

if protocol is sqlite3.PrepareProrocol: @H_301_13@

return "%f;%f" % (self.x,self.y) @H_301_13@

@H_301_13@

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

cur = con.cursor() @H_301_13@

@H_301_13@

p = Point(4.0,-3.2) @H_301_13@

cur.execute("select ?",(p,)) @H_301_13@

print cur.fetchone()[0] @H_301_13@

@H_301_13@

13.13.4 .2.2 注册一个适配器调用

另外一种可能是创建一个函数来将这种类型转换为字符串替代,并且使用register_adapter注册这个函数 @H_301_13@

注意:进行适配的类型/类必须是一种新样式(new-style)的类,也即,它必须有对象作为其基类。 @H_301_13@

@H_301_13@

import sqlite3 @H_301_13@

@H_301_13@

class Point(object): @H_301_13@

def __init__(self,y @H_301_13@

@H_301_13@

def adapt_point(point): @H_301_13@

return "%f;%f" % (point.x,point.y) @H_301_13@

@H_301_13@

sqlite3.register_adapter(Point,adapt_point) @H_301_13@

@H_301_13@

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

cur = con.cursor() @H_301_13@

@H_301_13@

p = Point(4.0,)) @H_301_13@

print cur.fetchone()[0] @H_301_13@

@H_301_13@

对于Python的内置的datetime.datedatetime.datetime类型,sqlite3模块有两个默认的适配器。现在我们假设要按照Unix的时间戳而不是ISO替代来存储datetime.datetime对象。 @H_301_13@

@H_301_13@

import sqlite3 @H_301_13@

import datetime,time @H_301_13@

@H_301_13@

def adapt_datetime(ts): @H_301_13@

return time.mktime(ts.timetuple()) @H_301_13@

@H_301_13@

sqlite3.register_adapter(datetime.datetime,adapt_datetime) @H_301_13@

@H_301_13@

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

cur = cur.cursor() @H_301_13@

@H_301_13@

now = datetime.datetime.now() @H_301_13@

cur.execute("select ?",(now,)) @H_301_13@

print cur.fetchone()[0] @H_301_13@

@H_301_13@

13.13.4 .3 sqlite值转换为定制的Python类型

写一个适配器可以让你吧定制的Python类型存入到sqlite。但是要使它真正有用,我们需要完成Pythonsqlite再到Python的双向工作。 @H_301_13@

进入转换器。 @H_301_13@

让我们再次回到Point类。我们使用分号分割的字符串在sqlite中储存了xy坐标。 @H_301_13@

首先,我们会定义一个转换函数,接受这个字符串作为参数,并且从这个字符串构建一个Point对象。 @H_301_13@

注意:转换函数总是以字符串作为参数被调用,而不管你以什么方式将数据存入sqlite @H_301_13@

注意:转换函数对大小写敏感。 @H_301_13@

@H_301_13@

def convert_point(s): @H_301_13@

x,y = map(float,s.split(";")) @H_301_13@

return Point(x,y) @H_301_13@

@H_301_13@

现在你需要让sqlite3模块知道,你从数据库中取出的实际上是一个point。有两种方法可以做到这一点: @H_301_13@

l 通过声明隐式进行 @H_301_13@

l 通过列名显式进行 @H_301_13@

两种方法都在“模块常量(Module Constants)”中描述, 13.13.1 章,在常量PARSE_DECLTYPESPARSE_COLNAMES的记录中。 @H_301_13@

下面的例子说明了两种方法 @H_301_13@

@H_301_13@

import sqlite3 @H_301_13@

@H_301_13@

class Point(object): @H_301_13@

def __init__(self,y @H_301_13@

def __repr__(self): @H_301_13@

return "(%f;%f)" % (self.x,self.y) @H_301_13@

@H_301_13@

def adapt_point(point): @H_301_13@

return "%f;%f" % (point.x,point.y) @H_301_13@

@H_301_13@

def convert_point(s): @H_301_13@

x,y) @H_301_13@

@H_301_13@

# Register the adapter @H_301_13@

sqlite3.register_adapter(Point,adapt_point) @H_301_13@

@H_301_13@

# Register the converter @H_301_13@

sqlite3.register_converter("point",convert_point) @H_301_13@

@H_301_13@

p = Point(4.0,-3.2) @H_301_13@

@H_301_13@

######################## @H_301_13@

# 1) Using declared types @H_301_13@

con = sqlite3.connect(":memory:",detect_types=sqlite3.PARSE_DECLTYPES) @H_301_13@

cur = con.cursor() @H_301_13@

cur.execute("create table test(p point)") @H_301_13@

@H_301_13@

cur.execute("insert into test(p) values (?)",)) @H_301_13@

cur.execute("select p from test") @H_301_13@

print "with declared types:",cur.fetchone()[0] @H_301_13@

cur.close() @H_301_13@

con.close() @H_301_13@

@H_301_13@

################# @H_301_13@

# 2) Using column names @H_301_13@

con = sqlite3.connect(":memory:",detect_types=sqlite3.PARSE_COLNAMES) @H_301_13@

cur = con.cursor() @H_301_13@

cur.execute("create table test(p)") @H_301_13@

@H_301_13@

cur.execute("insert into test(p) values (?)",)) @H_301_13@

cur.execute('select p as "p [point]" from test') @H_301_13@

print "with column names:",cur.fetchone()[0] @H_301_13@

cur.close() @H_301_13@

cur.close() @H_301_13@

@H_301_13@

13.13.4 .4 默认的适配器和转换器

datetime模块中有对datedatetime类型有默认的适配器。它们会被作为ISO日期或ISO时间戳送入sqlite中。 @H_301_13@

默认的转换器以"date"名字对应datetime.date,和"timestamp"名字对应datetime.datetime注册 @H_301_13@

这样,在大多数情况下,你可以从Python中使用date/timestamps,而不用做任何多余的工作。转换器的格式也与sqlite的实验性的date/time函数保持兼容。 @H_301_13@

下面的例子演示了这一点: @H_301_13@

@H_301_13@

import sqlite3 @H_301_13@

import datetime @H_301_13@

@H_301_13@

con = sqlite3.connect(":memory:",detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES) @H_301_13@

cur = con.cursor() @H_301_13@

cur.execute("create table test(d date,ts timestamp)") @H_301_13@

@H_301_13@

today = datetime.date.today() @H_301_13@

now = datetime.datetime.now() @H_301_13@

@H_301_13@

cur.execute("insert into test(d,ts) values (?,?)",(today,now)) @H_301_13@

cur.execute("select d,ts from test") @H_301_13@

row = cur.fetchone() @H_301_13@

print today,"=>",row[0],type(row[0]) @H_301_13@

print now,row[1],type(row[1]) @H_301_13@

@H_301_13@

cur.execute('select current_date as "d [date]",current_timestamp as "ts [timestamp]"') @H_301_13@

row = cur.fetchone() @H_301_13@

print "current_date",type(row[0]) @H_301_13@

print "current_timestamp",type(row[1]) @H_301_13@

@H_301_13@

13.13.5 交易控制

默认情况下,sqlite3模块在一个数据变更语言(Data Modification Language-DML)(i.e. INSERT/UPDATE/DELETE/REPLACE)语句之前,隐式的打开交易,并且在一个非DML,非查询(i.e. 任何非SELECT/INSERT/UPDATE/DELETE/REPLACE)语句之前,提交交易。 @H_301_13@

所以,如果你正处于一个交易之中,并且提交一个像CREATE TABLE ...,VACUUM,PRAGMA这样的命令,sqlite3模块将在执行这个名之前,隐式的提交者个交易。有两种原因要这样做。一是,这些命令中的一部分不能在交易中工作。二是pysqlite需要对交易保持跟踪(一个交易是否活跃)。 @H_301_13@

你可以控制pysqlite隐式的执行那种"BEGIN"语句,通过connect调用isolation_level参数,或者通过连接的isolation_level属性 @H_301_13@

如果你希望是自动执行模式,就把isolation_level设置为None @H_301_13@

否则,就采用默认设置,这将会产生普通的"BEGIN"语句,或者将其设置为sqlite支持isolation级别:DEFERRED,IMMEDIATE或者EXCLUSIVE @H_301_13@

因为sqlite3模块需要对交易保持跟踪,所以你不能在你的sql中使用OR ROLLBACK或者ON CONFLICT ROLLBACK。取而代之,你需要捕捉IntegrityError错误,并且自己调用连接的rollback方法 @H_301_13@

13.13.6 高效的使用pysqlite

13.13.6 .1 使用快捷方法

使用连接对象(Connection object)的非标准的execute,executemanyexecutescript方法,你可以书写更简洁的代码,因为你不用显式的(通常是冗余的)创建游标对象。这些快捷方法会隐式的创建并返回游标对象。这样你就可以直接使用对连接对象的单一单一调用,来执行一个SELECT语句,并对其进行迭代。 @H_301_13@

@H_301_13@

import sqlite3 @H_301_13@

@H_301_13@

persons = [ @H_301_13@

("Hugo","Boss"),@H_301_13@

("Calvin","Klein") @H_301_13@

] @H_301_13@

@H_301_13@

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

@H_301_13@

# Create ghe table @H_301_13@

con.execute("create table person(firstname,lastname)") @H_301_13@

@H_301_13@

# Fill the table @H_301_13@

con.executemany("insert into person(firtname,lastname) values (?,persons) @H_301_13@

@H_301_13@

# Print the table contents @H_301_13@

for row in con execute("select firstname,lastname from person"): @H_301_13@

print row @H_301_13@

@H_301_13@

# Using a dummy WHERE clause to not let sqlite take the shortcut table deletes. @H_301_13@

print "I just deleted",con.execute("delete from person where 1=1").rowcount,"rows") @H_301_13@

@H_301_13@

13.13.6 .2 通过名字而不是索引来访问列

sqlite3模块的一个有用的特性是其内置的sqlite3.Row类,它被设计用来作为一个行的代理。 @H_301_13@

使用这个类包装的行,既可以通过索引(有点像元组)来访问,也可以通过大小写敏感的名字来访问。 @H_301_13@

@H_301_13@

import sqlite3 @H_301_13@

@H_301_13@

con = sqlite3.connect("mydb") @H_301_13@

con.row_factory = sqlite3.Row @H_301_13@

@H_301_13@

cur = con.cursor() @H_301_13@

cur.execute("select name_last,age from people) @H_301_13@

for row in cur: @H_301_13@

assert row[0] == row["name_last"] @H_301_13@

assert row["name_last"] == row["nAmE_1AsT"] @H_301_13@

assert row[1] == row["age"] @H_301_13@

assert row[1] == row["Age"] @H_301_13@

@H_301_13@ 本文档由sharkw翻译,转载请注明出处--http://blog.csdn.net/sharkw

猜你在找的Sqlite相关文章