0

I am trying to make a query for my PostgreSQL database.

I think the format of my query is wrong and I can't seem to get it to work, I have posted my code below:

 query = cur.execute('SELECT "KINASE_NAME" FROM public."Phosphosite_table"
    WHERE "GENE_NAME" LIKE %(genename)s AND "RESIDUE" LIKE %(location)s')

The aim is to take the kinase name is the gene name and location match.

my error message appears as the following:

    ProgrammingError                          Traceback (most recent call last)
<ipython-input-33-9eae43b913d6> in <module>()
     35 cur = connection.cursor()
     36 
---> 37 query = cur.execute('SELECT "KINASE_NAME" FROM public."Phosphosite_table" WHERE "GENE_NAME" LIKE%(genename)s AND "RESIDUE" LIKE %(location)s')

Thanks!

Connor

2
  • Sorry, just to add, the variable is a list of strings that contain numbers within the strings if that helps. Commented Feb 12, 2019 at 14:31
  • 1
    Please tag your request with the API you are using. According to Lutz Horn it is psycopg (or psycopg2)? Commented Feb 12, 2019 at 14:58

1 Answer 1

1

Don't use string operations to build SQL queries. Use the proper %s syntax.

genname = "foo"
location = "bar"
cur.execute("SELECT ... LIKE %s and ... LIKE %s", (genname, location))

No quotes around the values must be used. The quoting will be done by the DB API library.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, Lutz, for your response. I tried the code that you posted: cur.execute('SELECT "KINASE_NAME" FROM public."Phosphosite_table" LIKE %s and LIKE %s', (genename, location)). and got an error: ProgrammingError Traceback (most recent call last) <ipython-input-38-d21250cd0d10> in <module>() ProgrammingError: syntax error at or near "LIKE" LINE 1: ...ECT "KINASE_NAME" FROM public."Phosphosite_table" LIKE '[''1...
... FROM public."Phosphosite_table" WHERE "GENE_NAME" LIKE ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.