简体   繁体   English

Sqlalchemy可以很好地处理多个附加的SQLite数据库文件吗?

[英]Can Sqlalchemy work well with multiple attached SQLite database files?

It's possible to connect multiple SQLite databases together using the 'ATTACH' statement and work with them jointly. 可以使用“ATTACH”语句将多个SQLite数据库连接在一起,并共同使用它们。 Tables in each SQLite file can be referenced using a schema/file specific keyword. 可以使用特定于架构/文件的关键字引用每个SQLite文件中的表。 This should allow you to simultaneously deal with multiple tables with the same name by scoping by file. 这应该允许您通过文件范围来同时处理具有相同名称的多个表。 I went through a very good tutorial on how to do this is here: 我经历了一个关于如何做到这一点的非常好的教程:

http://longweekendmobile.com/2010/05/29/how-to-attach-multiple-sqlite-databases-together/ http://longweekendmobile.com/2010/05/29/how-to-attach-multiple-sqlite-databases-together/

It seemed like I should be able to use SQLAlchemy's Table 'schema' keyword to differentiate between connections to multiple files. 看起来我应该能够使用SQLAlchemy的表'schema'关键字来区分多个文件的连接。 When I went searching for a way to use SQLAlchemy with SQLite databases that had been connected via ATTACH, this was the only example I found. 当我寻找一种方法来使用SQLAlchemy和通过ATTACH连接的SQLite数据库时,这是我找到的唯一例子。 Unfortunately, it is out of date and does not seem to work with current versions. 不幸的是,它已过时,似乎不适用于当前版本。

https://groups.google.com/forum/#!topic/sqlalchemy/QXqs4M2MjbY https://groups.google.com/forum/#!topic/sqlalchemy/QXqs4M2MjbY

I tried to update that example using Declarative classes, etc. Here was my attempt: 我尝试使用Declarative类等更新该示例。这是我的尝试:

from sqlalchemy import *
from sqlalchemy.orm import * 
from sqlalchemy.ext.declarative import *
#from sqlalchemy.pool import SingletonThreadPool

metadata = MetaData(object)
DeclarativeBase = declarative_base(metadata=metadata)

##########################################################################
# Classes
##########################################################################

class A(DeclarativeBase):
    __table__              = Table('A', DeclarativeBase.metadata,
                                   Column('id', Integer, primary_key=True, index=True, autoincrement=True),
                                   Column('col_a', Integer, index=True))

class B(DeclarativeBase):
    __table__              = Table('B', DeclarativeBase.metadata,
                                   Column('id', Integer, primary_key=True, index=True, autoincrement=True),
                                   Column('col_b', Integer, index=True),
                                   schema='database_b')

#engine = create_engine('sqlite:////tmp/database_a.sqlite',echo=True, poolclass=SingletonThreadPool)
engine = create_engine('sqlite:////tmp/database_a.sqlite',echo=True)
db     = engine.connect()
db.execute("ATTACH DATABASE '/tmp/database_b.sqlite' AS database_b")

DeclarativeBase.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()
session.commit()

Unfortunately, I got the following output: 不幸的是,我得到了以下输出:

monster:tmp ladmin$ python sqliteattachtest2.py 
2014-04-12 18:04:58,845 INFO sqlalchemy.engine.base.Engine ATTACH DATABASE '/tmp/database_b.sqlite' AS database_b
2014-04-12 18:04:58,845 INFO sqlalchemy.engine.base.Engine ()
2014-04-12 18:04:58,846 INFO sqlalchemy.engine.base.Engine PRAGMA "database_b".table_info("B")
2014-04-12 18:04:58,846 INFO sqlalchemy.engine.base.Engine ()
2014-04-12 18:04:58,846 INFO sqlalchemy.engine.base.Engine ROLLBACK
Traceback (most recent call last):
  File "sqliteattachtest2.py", line 29, in <module>
    DeclarativeBase.metadata.create_all(engine)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/schema.py", line 2793, in create_all
    tables=tables)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1479, in _run_visitor
    conn._run_visitor(visitorcallable, element, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1122, in _run_visitor
    **kwargs).traverse_single(element)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/sql/visitors.py", line 111, in traverse_single
    return meth(obj, **kw)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/engine/ddl.py", line 57, in visit_metadata
    if self._can_create_table(t)]
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/engine/ddl.py", line 35, in _can_create_table
    table.name, schema=table.schema)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/base.py", line 716, in has_table
    cursor = _pragma_cursor(connection.execute(statement))
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 662, in execute
    params)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 805, in _execute_text
    statement, parameters
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 874, in _execute_context
    context)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1024, in _handle_dbapi_exception
    exc_info
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 195, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 867, in _execute_context
    context)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 324, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.OperationalError: (OperationalError) unknown database "database_b" 'PRAGMA "database_b".table_info("B")' ()

I read that this sort of thing can be done using Postgres schemas. 我读到这种事情可以使用Postgres模式完成。 Multiple attached SQLite databases seems like a natural counterpart. 多个附加的SQLite数据库似乎是一个自然的对应物。 I suspect I'm either doing something stupid or missed some important point. 我怀疑我要么做些蠢事,要么错过了一些重要的观点。 Is it possible to use SQLAlchemy to work with multiple SQLite files at the same time? 是否可以使用SQLAlchemy同时处理多个SQLite文件? If so, what's the best way to do it? 如果是这样,最好的方法是什么? Are there other ORMs that make this easier than SQLAlchemy does? 还有其他ORM使这比SQLAlchemy更容易吗?

Thanks! 谢谢! Dan

Create Engine of SQlite with inmemory than attach different database files 使用内存创建SQlite引擎,而不是附加不同的数据库文件

from sqlalchemy import create_engine, MetaData, Table,Column,Integer,select
from sqlalchemy.orm import mapper, sessionmaker
from sqlite3 import dbapi2 as sqlite
from sqlalchemy.engine.reflection import Inspector

class Bookmarks(object):
    pass

class BookmarksB(object):
    pass



def loadSession():
    engine = create_engine('sqlite://', echo=True)
    engine.execute("attach database 'database_b' as BB;")
    engine.execute("attach database 'database_a' as AA;")
    metadata = MetaData(engine)


    inspector = Inspector.from_engine(engine)
    print inspector.get_table_names()

    moz_bookmarks = Table('table_a', metadata,Column("id", Integer, primary_key=True),schema='AA', autoload=True)
    mapper(Bookmarks, moz_bookmarks)
    moz_bookmarksB = Table('table_b', metadata,Column("id", Integer, primary_key=True),schema='BB', autoload=True)
    mapper(BookmarksB, moz_bookmarksB)

    Session = sessionmaker(bind=engine)
    session = Session()
    return session

if __name__ == "__main__":
    session = loadSession()
    res = session.query(Bookmarks).all()
    for m in res:
        print m.msisdn,m.id

    #print list(select([moz_bookmarks, moz_bookmarksB], moz_bookmarks.c.b_id == moz_bookmarksB.c.id).execute())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQLAlchemy占位符无法正常工作 - SQLAlchemy placeholder do not work well SQLAlchemy 和 SQLite:数据库被锁定 - SQLAlchemy and SQLite: database is locked 无法通过 Gino(异步 sqlalchemy 包装器)连接到 SQLite 数据库 - Can't connect to SQLite database through Gino (async sqlalchemy wrapper) 如何在指定为“SQLALCHEMY_BINDS”的所有数据库和默认数据库以及使用烧瓶 sqlalchemy 绑定中创建数据库模型 - How can I create database models across all the databases specified into "SQLALCHEMY_BINDS" and default database as well using flask sqlalchemy binds 使用SQLAlchemy动态附加SQLite数据库 - Dynamically attaching a SQLite database with SQLAlchemy SQLAlchemy-多个数据库问题 - SQLAlchemy - Multiple Database Issues 发送多个文件附件邮件 - Send multiple files attached mail sqlite3 在 centos7 和 python shell 中运行良好,但在 Uwsgi 中无法运行 - sqlite3 works well in centos7 and python shell,but can't work in Uwsgi SQLAlchemy对象已经连接到会话,然后刷新时出现DetachedInstanceError,仍然添加到数据库中 - SQLAlchemy Object is already attached to session, then DetachedInstanceError on refresh, still adds to database 使用sqlalchemy将列表/元组存储在sqlite数据库中 - storing list/tuples in sqlite database with sqlalchemy
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM