Python psycopg2 PostgreSQL简单的SELECT查询给出了语法错误

时间:2017-03-24 14:54:22

标签: python postgresql psycopg2

我正在尝试浏览SQL文件中的每个查询,并使用psycopg2在我的python脚本中执行它。每个查询都有一个我在执行之前替换的id。

sql文件中的第一个查询如下:

select * from subscriber where org_id = '1111111111';

我得到了旧ID并将其替换为我正在寻找的新ID

id_regex = re.compile("\d{10,}")
m = id_regex.search(q)
old_id = m.group(0)
new_q = q.replace(old_id, new_id)

然后我按以下方式执行查询

for index, cmd in enumerate(cmds):
        # ... (other stuff here)
        elif cmd != '\n':
            new_cmd = p_helper.replace_id(org_id, cmd)
            logger.debug("Running Command:\n" + new_cmd)
            try:
                if not test_run:
                    db_cursor.execute(new_cmd)
            except psycopg2.Error as e:
                logger.error(e.pgerror)
        else:
            pass
            # DO NOTHING

当我运行程序时,我收到以下错误:

ERROR:  syntax error at or near "select"
LINE 1: select * from subscriber where org_id = '9999999999';
        ^

第一次运行后的每个查询都没有

ERROR:  current transaction is aborted, commands ignored until end of transaction block

我在psql中手动运行了选择查询,它运行得很好,所以我不认为问题是语句的语法。我认为它与psycopg2所采用的查询格式有关。我不确定要改变什么,我已经查看了其他SO帖子,无法弄清楚我需要改变什么。如果有人可以帮助我解决这个问题,那就太棒了。谢谢!

版本

python: 2.7.6

psycopg2: 2.4.5

0 个答案:

没有答案