web*_*org 34 python sqlite serialization
我想将Python对象存储到SQLite数据库中.那可能吗?
如果是这样的话会有什么链接/例子呢?
nos*_*klo 59
您无法将对象本身存储在数据库中.您所做的是存储来自对象的数据并在以后重建它.
一个好方法是使用优秀的SQLAlchemy库.它允许您将定义的类映射到数据库中的表.每个映射的属性都将被存储,并可用于重建对象.查询数据库会返回类的实例.
有了它,您不仅可以使用sqlite,还可以使用大多数数据库 - 它目前还支持Postgres,MySQL,Oracle,MS-SQL,Firebird,MaxDB,MS Access,Sybase,Informix和IBM DB2.并且您可以让您的用户选择她想要使用的那个,因为您基本上可以在这些数据库之间切换而无需更改代码.
还有很多很酷的功能 - 比如自动JOINs,多态...
您可以运行一个快速,简单的示例:
from sqlalchemy import Column, Integer, Unicode, UnicodeText, String
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from random import choice
from string import letters
engine = create_engine('sqlite:////tmp/teste.db', echo=True)
Base = declarative_base(bind=engine)
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(Unicode(40))
address = Column(UnicodeText, nullable=True)
password = Column(String(20))
def __init__(self, name, address=None, password=None):
self.name = name
self.address = address
if password is None:
password = ''.join(choice(letters) for n in xrange(10))
self.password = password
Base.metadata.create_all()
Session = sessionmaker(bind=engine)
s = Session()
Run Code Online (Sandbox Code Playgroud)
然后我可以像这样使用它:
# create instances of my user object
u = User('nosklo')
u.address = '66 Some Street #500'
u2 = User('lakshmipathi')
u2.password = 'ihtapimhskal'
# testing
s.add_all([u, u2])
s.commit()
Run Code Online (Sandbox Code Playgroud)
这会对INSERT数据库运行语句.
# When you query the data back it returns instances of your class:
for user in s.query(User):
print type(user), user.name, user.password
Run Code Online (Sandbox Code Playgroud)
该查询将运行SELECT users.id AS users_id, users.name AS users_name, users.address AS users_address, users.password AS users_password.
打印结果如下:
<class '__main__.User'> nosklo aBPDXlTPJs
<class '__main__.User'> lakshmipathi ihtapimhskal
Run Code Online (Sandbox Code Playgroud)
所以你有效地将对象存储到数据库中,这是最好的方法.
mik*_*iku 15
是的,这是可能的,但有不同的方法,哪一个是合适的,将取决于您的要求.
酸洗
您可以使用pickle模块序列化对象,然后将这些对象存储在sqlite3(或文本字段,如果转储是例如base64编码)的blob中.注意一些可能的问题:问题/ 198692/can-i-pickle-a-python-dictionary-into-a-sqlite3-text-field
对象 - 关系映射
您可以使用对象关系映射.这实际上创建了可以在编程语言(维基百科)中使用的"虚拟对象数据库" .对于python,有一个很好的工具包:sqlalchemy.
小智 12
我很惊讶没有人阅读SQLite 3 库的文档,因为它说您可以通过创建适配器和转换器来做到这一点。例如,假设我们有一个名为“ Point”的类,我们想要存储它并在选择它并使用数据库游标的fetchone方法返回它时返回它。让我们让模块知道你从数据库中选择的是一个点
from sqlite3 import connect, register_adaptor, register_converter
class Point:
def __init__(self, x, y):
self.x, self.y = x, y
def __repr__(self):
return "(%f;%f)" % (self.x, self.y)
def adapt_point(point):
return ("%f;%f" % (point.x, point.y)).encode('ascii')
def convert_point(s):
x, y = list(map(float, s.split(b";")))
return Point(x, y)
# Register the adapter
register_adapter(Point, adapt_point)
# Register the converter
register_converter("point", convert_point)
p = Point(4.0, -3.2)
# 1) Using declared types
con = connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES)
con.execute("create table test(p point)")
con.execute("insert into test(p) values (?)", (p,))
cur = con.execute("select p from test")
print("with declared types:", cur.fetchone()[0])
con.close()
# 1) Using column names
con = connect(":memory:", detect_types=sqlite3.PARSE_COLNAMES)
con.execute("create table test(p)")
con.execute("insert into test(p) values (?)", (p,))
cur = con.execute('select p as "p [point]" from test')
print("with column names:", cur.fetchone()[0])
con.close()
Run Code Online (Sandbox Code Playgroud)
你可以使用pickle.dumps,它的返回可选对象作为字符串,你不需要将它写入临时文件.
将对象的pickled表示作为字符串返回,而不是将其写入文件.
import pickle
class Foo:
attr = 'a class attr'
picklestring = pickle.dumps(Foo)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
31896 次 |
| 最近记录: |