简体   繁体   English

python中的复杂PQSQl查询

[英]complex PQSQl query in python

I need to create a pgsql db query in python and execute it from python. 我需要在python中创建一个pgsql db查询,然后从python执行它。 I am facing issue in creating that query. 我在创建该查询时遇到问题。

i need to create a query like this 我需要创建一个这样的查询

"select datetime, ask, bid, high, low, close from dashboard_forex WHERE symbol = 'EUR/USD'"

so i did like this 所以我确实是这样

symbol = "EUR/USD"
"select datetime, ask, bid, high, low, close from dashboard_forex WHERE symbol = '"+symbol+"'"

and worked !!! 工作

but now i want to create a query like this.. 但现在我想创建这样的查询。

"select datetime, ask, bid, high, low, close from public."TFX" WHERE symbol = 'EUR/USD'"

i need to get it as string. 我需要把它作为字符串。 I tried many way but nothing helped me. 我尝试了很多方法,但没有任何帮助。 i need to pass this string in another function. 我需要在另一个函数中传递此字符串。 here this query will be dynamic, i cant do it manually. 在这里,此查询将是动态的,我无法手动执行。 can anyone help me. 谁能帮我。 ??? ???

edit-1 - added actual image of db query. edit-1-添加了数据库查询的实际图像。 在此处输入图片说明

here both single and double quotes involved.. 这里涉及单引号和双引号。

USE str.format() method 使用str.format()方法

tb1 = r'TFX'
sym = r'EUR/USD'
query = """select datetime, ask, bid, high, low, close from public."{}" WHERE symbol = '{}' """.format(tb1, sym)

print(query)

output 输出

select datetime, ask, bid, high, low, close from public."TFX" WHERE symbol = 'EUR/USD' 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM