【问题标题】:How to create a temporary table for an existing table with sqlalchemy?如何使用 sqlalchemy 为现有表创建临时表?
【发布时间】:2013-12-03 17:18:04
【问题描述】:

我有一个与以下类关联的表(在 mysql-server 中):

class Number(Base):
    __table_name__ = 'number'
    col0 = Column(...) # PRIMARY KEY
    col1 = Column(...)
    col2 = Column(...)
    col3 = Column(...)

我想在程序启动时为这个号码表创建一个临时表,这个临时表如下:

class tempNumber(Base):
      __table_name__ = 'temp_number'
      col0 = Column(...) # direct from Number.col0
      col1 = Column(...) # from Number.col1, but will be modified, for example tempNumber.col1 = Number.col1.replace(' ','')

我还尝试将以下行添加到班级编号:

__table_args__ = {'prefixes': ['TEMPORARY']}

但我的 python 程序告诉我表 temp_number 不存在。

还有一个问题是,如何快速复制数据?因为表号中有很多行。

【问题讨论】:

    标签: python mysql sqlalchemy temp-tables


    【解决方案1】:
    1. temp_number表是否真的存在于底层数据库中?

    2. 通常,通过sqlalchemy 无法快速将数据从一个表复制到另一个表,这在此类任务中具有很大的开销。相反,它会因您的底层数据库而异。

    例如,here's how one would do it in MySQLHere's a way to do it with PostgreSQL. 但是,这通常不会复制索引之类的东西。那,您必须通过数据库驱动程序手动设置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-24
      • 1970-01-01
      • 2023-03-24
      • 2016-12-03
      • 1970-01-01
      • 2017-12-26
      • 1970-01-01
      • 2021-05-18
      相关资源
      最近更新 更多