如何正确引用此字符串这个postgres sql语法
在我的python脚本中使用psycopg2' s cur.execute ("SELECT .. ")
Postgres sql:
SELECT 'ALTER TABLE RENAME ' || tablename || ' TO ' ||
REGEXP_REPLACE ( tablename, '_foo$', '_bar' ) || ';'
FROM pg_tables
WHERE tablename LIKE '%_foo';
在我的python脚本中:
cur.execute("SELECT 'ALTER TABLE RENAME ' || tablename || ' TO ' ||
REGEXP_REPLACE ( tablename, '_foo$', '_bar' ) || ';'
FROM pg_tables
WHERE tablename LIKE '%_foo'")
答案 0 :(得分:1)
只需添加换行符:
cur.execute("SELECT 'ALTER TABLE RENAME ' || tablename || ' TO ' || \
REGEXP_REPLACE ( tablename, '_foo$', '_bar' ) || ';' \
FROM pg_tables \
WHERE tablename LIKE '%_foo'")