【问题标题】:Using sqlalchemy to generate: SELECT * ... INTO OUTFILE "file";使用 sqlalchemy 生成:SELECT * ... INTO OUTFILE "file";
【发布时间】:2011-05-21 17:57:50
【问题描述】:

我最近开始使用 SQLALCHEMY 来查询一个 my-sql 数据库。我想生成一个使用“INTO OUTFILE <file>”语法将查询结果导出到测试文件的选择语句。例如:

SELECT *
FROM table
INTO OUTFILE '/tmp/export.txt';

有没有办法使用 SQLALCHEMY 生成“INTO OUTFILE...”子句?

如果不是,我可以继承其中一个 SQLALCHEMY 类,以便我自己构建该子句吗?

谢谢。

【问题讨论】:

    标签: mysql sqlalchemy into-outfile


    【解决方案1】:

    我对 SQLAlchemy 网站上的示例进行了一些思考和探索,然后弄清楚了。 (也发到sql-alchemy user reciptes


    from sqlalchemy import *
    from sqlalchemy.sql.expression import Executable, ClauseElement
    from sqlalchemy.ext import compiler
    
    class SelectIntoOutfile(Executable, ClauseElement):
        def __init__(self, select, file):
            self.select = select
            self.file = file
    
    
    @compiler.compiles(SelectIntoOutfile)
    def compile(element, compiler, **kw):
        return "%s INTO OUTFILE '%s'" % (
            compiler.process(element.select), element.file
        )
    
    
    e = SelectIntoOutfile(select([s.dim_date_table]).where(s.dim_date_table.c.Year==2009), '/tmp/test.txt')
    print e
    eng.execute(e)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-21
      • 2013-03-24
      • 1970-01-01
      • 2020-02-24
      • 2018-07-02
      • 1970-01-01
      • 1970-01-01
      • 2012-02-26
      相关资源
      最近更新 更多