0

我正在尝试运行“Python 标准库示例”一书中的以下“sqlite3_custom_type.py”示例。以下代码“开箱即用”:

import os
import sqlite3


db_filename = 'todo.db'

db_is_new = not os.path.exists(db_filename)

conn = sqlite3.connect(db_filename)

if db_is_new:
    print('need to create schema')
else:
    print('database exists, assume schema does to')

conn.close()

#import sqlite3
try:
    import cPickle as pickle
except:
    import pickle

db_filename = 'todo.db'

def adapter_func(obj):
    """Convert from in-memory to storage representation.
    """
    print 'adapter_func(%s)\n' % obj
    return pickle.dumps(obj)

def converter_func(data):
    """Convert from storage to in-memory representation.
    """
    print 'converter_func(%r)\n' % data
    return pickle.loads(data)


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.
sqlite3.register_adapter(MyObj, adapter_func)
sqlite3.register_converter("MyObj", converter_func)

# Create some objects to save.  Use a list of tuples so
# the sequence can be passed directly to executemany().
to_save = [ (MyObj('this is a value to save'),),
            (MyObj(42),),
            ]

with sqlite3.connect(db_filename,
                     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
    )
    """)
    cursor = conn.cursor()

    # Insert the objects into the database
    cursor.executemany("insert into obj (data) values (?)", to_save)

    # Query the database for the objects just saved
    cursor.execute("select id, data from obj")
    for obj_id, obj in cursor.fetchall():
        print 'Retrieved', obj_id, obj, type(obj)
        print   

但是,如果我将所有代码放在一个函数中,例如

def stuff():
    ~same code as above but indented
if __name__=="__main__":
    stuff()

然后我得到一个错误代码:

cursor.executemany("insert into obj (data) values (?)", to_save)
sqlite3.InterfaceError: Error binding parameter 0 - probably unsupported type.

为什么代码在函数中时不起作用,我怎样才能使它起作用?

4

3 回答 3

2

根据其他答案,将类放在模块范围内是一种很好的风格。但是,在这种特殊情况下失败的真正原因是因为pickle.dumps(obj)调用试图腌制非模块级别的类。

在您的 中尝试以下代码adapter_func

def adapter_func(obj):
    """Convert from in-memory to storage representation.
    """
    try:
        return pickle.dumps(obj)
    except Exception, arg:
        print 'Failed to pickle object [%s]' % arg

MyObj当在内部声明时,您将看到如下错误stuff

Failed to pickle object [Can't pickle <class '__main__.MyObj'>: it's not found as __main__.MyObj]

如pickle 文档中所述,pickle需要在模块级别声明要腌制的类。sqlite3 模块似乎正在压缩适配器函数中引发的异常,而不是通过传播它们导致静默失败。

你可以在里面声明和注册你的适配器和转换器函数stuff。除了样式问题,您还可以MyObj在函数内部声明并让它工作,只要您找到其他方法来序列化/反序列化您的对象。

这是试图腌制一个不在顶层的类,这是这个问题的根源。

于 2013-09-27T01:16:44.650 回答
1

不要将类和函数放在stuff. 特别是不要放在MyObj里面。

如果要使用 if __name__=="__main__":条件,只需将不是类或函数的代码放在stuff.

于 2013-09-26T05:52:57.653 回答
1

根据 Tichodroma 的回答,您需要从函数中取出所有类和函数stuff,包括sqlite3.register_adapterand sqlite3.register_converter。此外,作为一般风格点,您的导入应该放在脚本的顶部

以下代码有效:

import os
import sqlite3
try:
    import cPickle as pickle
except:
    import pickle

class MyObj(object):
    def __init__(self, arg):
        self.arg = arg
    def __str__(self):
        return 'MyObj(%r)' % self.arg

def adapter_func(obj):
    """Convert from in-memory to storage representation.
    """
    print('adapter_func(%s)\n' % obj)
    return pickle.dumps(obj)

def converter_func(data):
    """Convert from storage to in-memory representation.
    """
    print('converter_func(%r)\n' % data)
    return pickle.loads(data)

# Register the functions for manipulating the type.
sqlite3.register_adapter(MyObj, adapter_func)
sqlite3.register_converter("MyObj", converter_func)

def stuff():
    db_filename = 'todo.db'

    db_is_new = not os.path.exists(db_filename)

    conn = sqlite3.connect(db_filename)

    if db_is_new:
        print('need to create schema')
    else:
        print('database exists, assume schema does to')

    conn.close()

    db_filename = 'todo.db'

    # Create some objects to save.  Use a list of tuples so
    # the sequence can be passed directly to executemany().
    to_save = [ (MyObj('this is a value to save'),),
                (MyObj(42),),
                ]

    with sqlite3.connect(db_filename,
                         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
        )
        """)
        cursor = conn.cursor()

        # Insert the objects into the database
        cursor.executemany("insert into obj (data) values (?)", to_save)

        # Query the database for the objects just saved
        cursor.execute("select id, data from obj")
        for obj_id, obj in cursor.fetchall():
            print('Retrieved', obj_id, obj, type(obj))
            print()

if __name__ == "__main__":
    stuff()
于 2013-09-26T14:43:59.510 回答