本作品采用知识共享署名-非商业性使用-相同方式共享 3.0 Unported许可协议进行许可。允许非商业转载,但应注明作者及出处。
作者:liuyuan_jq
2011-03-28
################################################################################ class MyObj(object): """ 自定义数据表的数据类型 """ def __init__(self, arg): self.arg = arg def __str__(self): return 'MyObj(%r)' % self.arg # Register the functions for manipulating the type. def adapter_func(obj): """memory -> storage 将自定义数据类型转化为SQLite支持的类型 """ print 'Saving:', obj # 实例化自定义对象 return pickle.dumps(obj) sqlite3.register_adapter(MyObj, adapter_func) ################################################################################ def converter_func(data): """storage -> memory""" return pickle.loads(data) sqlite3.register_converter("MyObj", converter_func)
conn = sqlite3.connect('type_demo.db', detect_types=sqlite3.PARSE_DECLTYPES) # Create a table with column of type "MyObj" conn.execute(""" create table if not exists obj ( id integer primary key autoincrement not null, data MyObj ) """) cursor = conn.cursor() # Insert the objects into the database print 'Inserting:' to_save = [ (MyObj('this is a value to save'),), (MyObj(42),), ] cursor.executemany( "insert into obj (data) values (?)", to_save)
# Query the database for the objects just saved print '/nQuerying:' cursor.execute("select id, data from obj") info = cursor.fetchall() pprint(info) for obj_id, obj in info: print 'Retrieved', obj_id, type(obj), obj
#!/usr/bin/env python # encoding: utf-8 """Defining a custom type. """ #from __future__ import with_statement import sqlite3 import cPickle as pickle from pprint import pprint ################################################################################ class MyObj(object): """ 自定义数据表的数据类型 """ def __init__(self, arg): self.arg = arg def __str__(self): return 'MyObj(%r)' % self.arg # Register the functions for manipulating the type. def adapter_func(obj): """memory -> storage 将自定义数据类型转化为SQLite支持的类型 """ print 'Saving:', obj # 实例化自定义对象 return pickle.dumps(obj) sqlite3.register_adapter(MyObj, adapter_func) ################################################################################ def converter_func(data): """storage -> memory""" return pickle.loads(data) sqlite3.register_converter("MyObj", converter_func) ################################################################################ #with sqlite3.connect( # 'type_demo.db', # detect_types=sqlite3.PARSE_DECLTYPES) as conn: # # # Create a table with column of type "MyObj" # conn.execute(""" # create table if not exists obj ( # id integer primary key autoincrement not null, # data MyObj # ) # """) conn = sqlite3.connect('type_demo.db', detect_types=sqlite3.PARSE_DECLTYPES) # Create a table with column of type "MyObj" conn.execute(""" create table if not exists obj ( id integer primary key autoincrement not null, data MyObj ) """) cursor = conn.cursor() # Insert the objects into the database print 'Inserting:' to_save = [ (MyObj('this is a value to save'),), (MyObj(42),), ] cursor.executemany( "insert into obj (data) values (?)", to_save) # Query the database for the objects just saved print '/nQuerying:' cursor.execute("select id, data from obj") info = cursor.fetchall() pprint(info) for obj_id, obj in info: print 'Retrieved', obj_id, type(obj), obj