varun kumar
varun kumar

Reputation: 25

cx_Oracle SELECT statement with WHERE clause and single quote

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?

WHat is wrong here , when I using single quote around username then I am getting this error .

Upvotes: 0

Views: 298

Answers (2)

Artur Costa Tourinho
Artur Costa Tourinho

Reputation: 36

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

Upvotes: 2

Anthony Tuininga
Anthony Tuininga

Reputation: 7096

The syntax for your SQL statement is indeed incorrect. It isn't clear what the value of USERNAME is supposed to be. If it is just supposed to be the connected user you can just do this:

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

If you intend that as a parameter you can do this:

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

Upvotes: 1

Related Questions