【问题标题】:cx_Oracle SELECT statement with WHERE clause and single quote带有 WHERE 子句和单引号的 cx_Oracle SELECT 语句
【发布时间】:2022-08-15 04:13:38
【问题描述】:
import cx_Oracle

dsn_tns = cx_Oracle.makedsn(\'**********\', \'*******\', service_name=\'***\') 

conn = cx_Oracle.connect(user=\'******\', password=\'*******\', dsn=dsn_tns)

c = conn.cursor()
c.execute(\'select username,created from dba_users where username=\'USERNAME\' \')
for row in c:
    print (row[0], \'-\', row[1]) # this only shows the first two columns. To add an additional column you\'ll need to add , \'-\', row[2], etc.
conn.close()
  File \"*******************\", line 9
    c.execute(\'select username,created from dba_users where username=\'MONITOR\' \')
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
SyntaxError: invalid syntax. Perhaps you forgot a comma?

这里出了什么问题,当我在用户名周围使用单引号时,我收到了这个错误。

    标签: python oracle cx-oracle


    【解决方案1】:
    c.execute(
        """select username, created from dba_users where username=:USERNAME""", 
        USERNAME = 'Cristiano Ronaldo'
    )
    

    【讨论】:

    • 这对我有用。非常感谢。这里的三重双引号概念是什么?
    • 它允许多行字符串。
    【解决方案2】:

    您的 SQL 语句的语法确实不正确。目前尚不清楚 USERNAME 的值应该是什么。如果它只是应该是连接的用户,你可以这样做:

    c.execute("select username, created from dba_users where username = user")
    

    如果您打算将其作为参数,则可以执行以下操作:

    c.execute("select username, created from dba_users where username = :user",
              user=USERNAME)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-14
      • 2018-03-08
      • 1970-01-01
      • 2016-03-25
      • 2015-09-28
      • 2010-12-07
      • 2013-07-07
      相关资源
      最近更新 更多