如何动态地从字典值字符串中删除引号
How to remove quotes from dictionary value string dynamically
我有一本像这样的字典: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 是 class 基于 pyexasol 构建的,只有连接参数。 class的相关方法有:
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
)
正如我在评论中所说,您需要阅读 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;
希望对您有所帮助。
我有一本像这样的字典: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 是 class 基于 pyexasol 构建的,只有连接参数。 class的相关方法有:
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
)
正如我在评论中所说,您需要阅读 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;
希望对您有所帮助。