1

我有一本像这样的字典:d = {'table': 'db_schema.foo', 'column': 'bar'}. 我想在从所有值中删除引号后传递这本字典。所以我想{'table': db_schema.foo, 'column': bar}动态获取。

细节:

我试图在连接后将运行动态查询传递给 exasol。我在这里使用 pyexasol 并尝试execute()。我正在尝试执行简单的查询,例如:

SELECT * 
FROM db_schema.foo
WHERE bar >= 0

如果我提供字典d,那么执行的查询是:

SELECT * 
FROM 'db_schema.foo'
WHERE 'bar' >= 0

并导致错误。所以我想从所有值中删除引号。我已经尝试过{k:v.strip("\'") for k, v in d.items()}等,但还没有成功。

要求的代码:

foo_query = '''select *
                from {app_table}
                where {col} >= '2020-01-01'
                limit 10;'''
foo_param={'app_table': 'src.application',
             'col': 'createdat'}
foo_results = exasol.runQueryExaParams(query=foo_query, query_param=foo_param)
foo_results1 = exasol.runQueryExaParams(query=foo_query, query_param={k:v.strip("\'") for k, v in foo_param.items()})

其中 exasol 是一个建立在 pyexasol 之上的类,只有连接参数。类的相关方法有:

    def runQueryExaParams(self, query, query_param):
        self.conn = pyexasol.connect(
            dsn=self.__dsn, user=self.__user, password=self.__password, encryption=True)
        res = self.conn.export_to_pandas(query, query_param)
        self.conn.close()
        res.columns = res.columns.str.lower()
        return res

在这两种情况下,我都会遇到相同的错误:

pyexasol.exceptions.ExaQueryError: 
(
    message     =>  syntax error, unexpected simple_string_literal [line 3, column 22] (Session: )
    dsn         =>  
    user        =>  
    schema      =>  
    code        =>  42000
    session_id  =>  
    query       =>  EXPORT (
select *
                from 'src.application'
                where 'createdat' >= '2020-01-01'
                limit 10
) INTO CSV
AT 'https://1.2.3.4' FILE '000.csv'
WITH COLUMN NAMES
)
4

1 回答 1

2

正如我在评论中所说,您需要阅读SQL FORMATTING

话虽如此,您需要将代码更改为如下所示:

query = '''
    SELECT *
    FROM {app_table!q}
    WHERE {col!i} >= '2020-01-01'
    LIMIT 10;
'''

query_param = {
    'app_table': ('src', 'application'),
    'col': 'createdat'
}

results = exasol.runQueryExaParams(
    query=query,
    query_param=query_param
)

如文档所示,上面的代码将产生如下查询:

SELECT *
FROM "src"."application"
WHERE createdat >= '2020-01-01'
LIMIT 10;

我希望它有所帮助。

于 2020-01-30T12:15:26.950 回答