Python 动态列名称和值的字符串替换
Python string substitution for dynamic column name and values
我有以下查询从 PostgreSQL 检索数据。
select_stmt = "SELECT * FROM kart_user WHERE token = %(token)s"
cursor.execute(select_stmt, { 'token': 2 })
某些案例字典可能包含多个键和值
例如-
{ 'token': 10,'custid':100,cycle:"yes" }
在这种情况下,我如何动态指定列名及其值??
我试过了..
select_stmt = "SELECT * FROM kart_user WHERE dist.keys() = %(dist.keys())s"
cursor.execute(select_stmt, dist)
但运气不好。感谢您的回复..
根据您尝试过的语句,我假设您有三个键和三个值,因此您想对所有三个键一一执行 SELECT 语句。以下解决方案将对您有所帮助。
d = { 'token': 10,'custid':100,cycle:"yes" }
select_stmt = "SELECT * FROM kart_user WHERE {key} = '{value}'"
allowed_col = ['token', 'custid'] # Predefined column names to allow from coming dictionary.
for key, value in d.iteritems():
if key in allowed_col:
select_stmt = select_stmt.format(key=key, value=value)
cursor.execute(select_stmt, dist)
# Further fetching code you can execute. Here you will get select statement for particular key and value.
@对于和条件
d = { 'token': 10,'custid':100,'cycle':"yes" }
select_stmt = "SELECT * FROM kart_user WHERE"
allowed_col = ['token', 'custid'] # Predefined column names to allow from coming dictionary.
for key, value in d.iteritems():
if key in allowed_col:
condition = "{key} = '{value}'".format(key=key, value=value)
select_stmt = select_stmt + " and " + condition
cursor.execute(select_stmt, dist)
# Further fetching code you can execute. Here you will get select statement for particular key and value.
上述答案的一个线性解决方案,
d = { 'token': 10,'custid':100,'cycle':"yes" }
condition = ' and '.join([key+'="'+str(value)+'"' for key,value in d.iteritems() if key in allowed_col])
select_stmt = "SELECT * FROM kart_user WHERE"
select_stmt = " ".join([select_stmt, condition])
cursor.execute(select_stmt, dist)
# Further fetching code you can execute. Here you will get select statement for particular key and value.
如果您的查询与我回答的不同,请回复此问题。谢谢
我有以下查询从 PostgreSQL 检索数据。
select_stmt = "SELECT * FROM kart_user WHERE token = %(token)s"
cursor.execute(select_stmt, { 'token': 2 })
某些案例字典可能包含多个键和值
例如-
{ 'token': 10,'custid':100,cycle:"yes" }
在这种情况下,我如何动态指定列名及其值??
我试过了..
select_stmt = "SELECT * FROM kart_user WHERE dist.keys() = %(dist.keys())s"
cursor.execute(select_stmt, dist)
但运气不好。感谢您的回复..
根据您尝试过的语句,我假设您有三个键和三个值,因此您想对所有三个键一一执行 SELECT 语句。以下解决方案将对您有所帮助。
d = { 'token': 10,'custid':100,cycle:"yes" }
select_stmt = "SELECT * FROM kart_user WHERE {key} = '{value}'"
allowed_col = ['token', 'custid'] # Predefined column names to allow from coming dictionary.
for key, value in d.iteritems():
if key in allowed_col:
select_stmt = select_stmt.format(key=key, value=value)
cursor.execute(select_stmt, dist)
# Further fetching code you can execute. Here you will get select statement for particular key and value.
@对于和条件
d = { 'token': 10,'custid':100,'cycle':"yes" }
select_stmt = "SELECT * FROM kart_user WHERE"
allowed_col = ['token', 'custid'] # Predefined column names to allow from coming dictionary.
for key, value in d.iteritems():
if key in allowed_col:
condition = "{key} = '{value}'".format(key=key, value=value)
select_stmt = select_stmt + " and " + condition
cursor.execute(select_stmt, dist)
# Further fetching code you can execute. Here you will get select statement for particular key and value.
上述答案的一个线性解决方案,
d = { 'token': 10,'custid':100,'cycle':"yes" }
condition = ' and '.join([key+'="'+str(value)+'"' for key,value in d.iteritems() if key in allowed_col])
select_stmt = "SELECT * FROM kart_user WHERE"
select_stmt = " ".join([select_stmt, condition])
cursor.execute(select_stmt, dist)
# Further fetching code you can execute. Here you will get select statement for particular key and value.
如果您的查询与我回答的不同,请回复此问题。谢谢