2017-03-22 4 views
0

인터넷에 흩어져있는 이와 비슷한 몇 가지 문제가 있지만 내 특정 문제를 정확히 다루는 문제는 없습니다.pselopg2/python으로이 camelcase postgreSQL 쿼리를 처리하려면 어떻게해야합니까?

여러분이 도움을 줄 수 있기를 바랍니다. 당신이 마지막 SELECT 한 Statment에서 볼 수 있듯이, 내가 할 수없는 낙타 표기법으로 포맷 이름의 테이블 (에서 선택을 시도하고

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() 

: 여기

내 스크립트의 일부입니다 변경 : /).

나는 여러 가지 방법으로 AsIs 확장을 사용해 보았습니다.

나는 camelCase 변수를 매개 변수화하려고 시도했지만 테이블 이름을 사용하려고 할 때 문제가 발생합니다.

올바르게 연구 한 경우 'AsIs'로 매개 변수화하면 매개 변수 자체가 테이블/색인 가능 항목이 아닌 VALUE 일 때만 낙타 사례 문제 만 해결됩니다.

마지막으로,이 스크립트의 이유는 위의 쿼리를 사용하여 내 DB의 테이블을 업데이트 한 다음 다른 쿼리를 사용하여이 테이블에서 생성 된 전자 메일의 정보로 사용되는 데이터를 반환하는 것입니다 .

psql 명령, node.js 파일 또는 crontab에 설정할 수있는 다른 파일 형식을 사용하는 bash 스크립트에서 다른 방법으로이 쿼리를 실행하는 방법에 대한 제안이있는 경우 제안. 쿼리를이 파일에 남겨 둘 필요는 없지만 쿼리는이 파일에 남아 있어야합니다. (pgAgent는 선호하지 않습니다.)

저는 이것을 우분투 14.04 서버에서 실행하고 있습니다.

도움을 주시면 감사하겠습니다. 감사!

답변

1

식별자 주위의 큰 따옴표는 이스케이프 처리되지 않습니다. 블록 따옴표를 대신 사용하십시오.

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'))) 
""" 
+0

고마워요! 매력처럼 일했습니다. – jmoneygram

0
cur.execute("select * from %s", (AsIs('"MyTable"'),))