【问题标题】:cx_Oracle: Error 933. ORA-00933: “SQL command not properly ended”: SQL command error?cx_Oracle:错误 933。ORA-00933:“SQL 命令未正确结束”:SQL 命令错误?
【发布时间】:2017-10-07 19:29:09
【问题描述】:

之前有人问过类似的问题,但我仍然无法找到解决方案。我的代码:

    try:
        connection = cx_Oracle.connect(ORACLE_CONNECT)
        logger.info("Connection to Oracle success.")
        print ("Oracle DB version: " + connection.version)
        print ("Oracle client encoding: " + connection.encoding)
        print ("Python version: " + platform.python_version())
    except cx_Oracle.DatabaseError as e:
        error, = e.args
        if error.code == 1017:
            print ("Username/password invalid.")
            logger.debug("Username/password invalid: %s", error.code)
        else:
            logger.debug("Database connection error: %s", e)
            print ("Database connection error: %s".format(e))
        raise

    cursor = connection.cursor()
    smsreport_text_new = tuple(smsreport_text)
    find_command = self.identify_unique_msgid(smsreport_list)
    cursor.execute(find_command)

 def identify_unique_msgid(self, smsreport_list):
    msgid_i_to_be_crosschecked = smsreport_list.get('msgid_i')
    msgid_ii_to_be_crosschecked = smsreport_list.get('msgid_ii')
    find_command = 'SELECT * FROM myTable WHERE msgid_i = {0}'.format(msgid_i_to_be_crosschecked)
    print (find_command)

    return find_command

find_command 看起来像这样:

SELECT * FROM myTable WHERE msgid_i = 2R67C865FB6ZHG5A9

我尝试过在 SQL 查询末尾使用和不使用分号,但仍然失败。我知道连接有效,因为我有另一个查询(见下文),这会将数据写入表中。只是在尝试查找包含某些值的行时,我才会收到此错误消息。

insert into xura_push (date_sms, result_sms, msgid, msgparts, msgid_i, msgid_ii) values (TO_DATE(:1, 'dd-mon-yyyy hh24:mi:ss'), :2, :3, :4, :5, :6)

我哪里错了?

干杯,pymat。

【问题讨论】:

  • 使用参数而不是直接将值放入字符串中。更实际地,您需要在字符串常量周围加上单引号。
  • @GordonLinoff 你的意思是 find_command 应该类似于上面的插入命令?
  • 你可以使用这个我猜"SELECT * FROM myTable WHERE msgid_i = \'{0}\'"如果你不太在乎,但是是的,你应该使用参数化查询。
  • @SudiptaMondal 似乎有效。如果我正在查询,如何查看查询结果?该命令刚刚执行,但不知道从哪里开始。
  • @pymat 你可以使用db_records = cursor.fetchall(); 然后遍历db_records

标签: python sql oracle python-3.x cx-oracle


【解决方案1】:

如上面的 cmets 所述,使用如下参数:

def identify_unique_msgid(self, smsreport_list):
    msgid_i_to_be_crosschecked = smsreport_list.get('msgid_i')
    msgid_ii_to_be_crosschecked = smsreport_list.get('msgid_ii')
    find_command = 'SELECT * FROM myTable WHERE msgid_i = :msgid'
    return find_command, dict(msgid = msgid_i_to_be_crosschecked)

cursor = connection.cursor()
smsreport_text_new = tuple(smsreport_text)
find_command, args = self.identify_unique_msgid(smsreport_list)
cursor.execute(find_command, args)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-14
    • 1970-01-01
    • 2015-08-01
    • 2021-11-25
    • 2014-10-07
    • 1970-01-01
    • 2020-02-29
    相关资源
    最近更新 更多