Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用psycopg2/python处理这个camelcase postgreSQL查询?_Python_Postgresql_Ubuntu_Psycopg2 - Fatal编程技术网

如何使用psycopg2/python处理这个camelcase postgreSQL查询?

如何使用psycopg2/python处理这个camelcase postgreSQL查询?,python,postgresql,ubuntu,psycopg2,Python,Postgresql,Ubuntu,Psycopg2,有几个类似的问题分散在互联网上,但没有一个能准确地解决我的特定问题 希望你们能帮忙 以下是我脚本的一小部分: import psycopg2 as p import psycopg2.extras as e # The 'AsIs' extension is here because I've attempted using it to fix my issue - with no luck unfortunately... from psycopg2.extensions import AsI

有几个类似的问题分散在互联网上,但没有一个能准确地解决我的特定问题

希望你们能帮忙

以下是我脚本的一小部分:

import psycopg2 as p
import psycopg2.extras as e
# The 'AsIs' extension is here because I've attempted using it to fix my issue - with no luck unfortunately...
from psycopg2.extensions import AsIs

con = p.connect("dbname='my_db' user='user_name' host='123.45.67.89' port='5432'")

cur = con.cursor(cursor_factory=e.DictCursor)

query = "INSERT INTO happy_alerts_triggered (happy_affiliate_id, alert_format, alert_minutes, happy_client_internal_id, alert_triggered_date, alert_processed_date, alert_id, affiliate_name, client_name) 
SELECT r.happy_affiliate_id, r.alert_format, r.alert_minutes, r.happy_client_internal_id, now() as alert_triggered_date, null as alert_processed_date, r.id, ha.happy_affiliate_description, hc.happy_client_description 
FROM happy_alerts_config r 
INNER JOIN happy_affiliates ha ON r.happy_affiliate_id = ha.id 
INNER JOIN happy_clients hc ON r.happy_client_internal_id = hc.id 
WHERE not exists
(SELECT 1 FROM "happyEvents" he 
WHERE he."messageType" = r.alert_format 
AND he."affiliateClient" = hc.happy_client_description 
AND he."insertTime" > (now() - (r.alert_minutes * interval '1 minute')))"

cur.execute(query)

con.commit()
cur.close()
正如您在最后的SELECT语句中所看到的,我试图从一个名称格式为camelCase(无法更改:/)的表中进行选择

我尝试了AsIs扩展的几种不同方式,但没有成功

我已经尝试过参数化camelCase变量,但是在尝试使用表名进行参数化时会出现问题

如果我研究正确,使用“AsIs”进行参数化只会在参数本身是值而不是表/可索引项时修复camelCase问题

最后,使用此脚本的原因是更新my DB中的一个表(使用上面的查询),然后使用另一个查询从该表返回数据,这些数据将用作在此脚本中生成的电子邮件中的信息

如果有关于如何以其他方式运行此查询的建议(即在使用psql命令的bash脚本中、node.js文件或可设置为crontab的任何其他文件类型),我愿意听取建议。查询不必保留在此文件中,尽管我希望它保留。(pgAgent不是首选方法。)

我在Ubuntu 14.04服务器上运行这个


感谢您的帮助!谢谢

标识符周围的双引号不会转义。改为使用块引号

cur.execute("select * from %s", (AsIs('"MyTable"'),))
query = """
INSERT INTO happy_alerts_triggered (happy_affiliate_id, alert_format, alert_minutes, happy_client_internal_id, alert_triggered_date, alert_processed_date, alert_id, affiliate_name, client_name) 
SELECT r.happy_affiliate_id, r.alert_format, r.alert_minutes, r.happy_client_internal_id, now() as alert_triggered_date, null as alert_processed_date, r.id, ha.happy_affiliate_description, hc.happy_client_description 
FROM happy_alerts_config r 
INNER JOIN happy_affiliates ha ON r.happy_affiliate_id = ha.id 
INNER JOIN happy_clients hc ON r.happy_client_internal_id = hc.id 
WHERE not exists
(SELECT 1 FROM "happyEvents" he 
WHERE he."messageType" = r.alert_format 
AND he."affiliateClient" = hc.happy_client_description 
AND he."insertTime" > (now() - (r.alert_minutes * interval '1 minute')))
"""

非常感谢。工作得很有魅力。