2

I searched for a long time for the answer and did the following:

(1)

query = "SELECT COUNT(*) FROM %s WHERE user_name = %s" % (table_name, username)
result = conn.query(query).result()[0][0]
if result == 0:
    (do something)

(2)

query = "SELECT 1 FROM %s WHERE user_name = %s" %(table_name, username)
result = conn.query(query).result()[0][0]
if result == ' ':
    (do something)

(3)

query = "SELECT EXISTS (SELECT 1 FROM %s WHERE user_name = %s)" %(table_name, username)
result = conn.query(query).result()[0][0]
if result == 't':
    (do something)

But all don't work... the error is always:

column "Tom" does not exist

Since it really doesn't exist and I just want to check if it exists. Any help is appreciated.

3
  • Did you try this? stackoverflow.com/questions/6016132/… Commented Oct 19, 2012 at 17:36
  • @ronak Yes I tried..still column "Tom" does not exist error..... Commented Oct 19, 2012 at 17:38
  • What database connection library are you using? Commented Oct 20, 2012 at 4:13

3 Answers 3

3
sql = """SELECT count(*)
         FROM information_schema.columns
         WHERE table_name = '%s'
         AND column_name = '%s'
      """ % (thetable,thecolumn)
Sign up to request clarification or add additional context in comments.

1 Comment

I tried it and it gives me the error: column "information_table" does not exist where information_table is my table name. I checked and this table is already created. Any ideas?
2

You're not quoting your strings.

You query looks like this, once it gets to PostgreSQL:

SELECT COUNT(*) FROM Table WHERE user_name = Tom

Which compares the user_name column against the non-existant Tom column.

What you want is a query like this:

SELECT COUNT(*) FROM Table WHERE user_name = 'Tom'

To do this right, you should be using parameterized statements, to avoid any possibility of SQL injection. With a DBAPI module, it's a simple as:

cursor = conn.cursor()
cursor.execute('SELECT COUNT(*) FROM Table WHERE user_name = %s', user_name)

If you need the table name to be dynamic as well, then you have to construct it like this:

cursor = conn.cursor()
query = 'SELECT COUNT(*) FROM %s WHERE user_name = %%s' % table_name
cursor.execute(query, user_name)

But you need to be absolutely certain that the table name is valid. Anybody who could control that variable could do absolutely anything to your database.

3 Comments

in my query query = "SELECT COUNT(*) FROM %s WHERE user_name = %s" % (table_name, username), the table_name and username are already strings..so it is like table_name = "information_table", username = "Tom"...
It's "like" that, or is that somehow the actual query that is being sent to the database? I strongly suspect that it is not. What do you get if you print repr(username) in Python?
I think this is the problem...I did make some mistakes like username = Tom. So Actually I changed the sentence "SELECT COUNT(*) FROM %s WHERE username = %s" % (table_name, username) to "SELECT COUNT(*) FROM %s WHERE username = '%s'" %(table_name, username), and problem solved...
1

Always remember: EAFP

try:
   result = conn.query(query).result()
except ProgrammingError:  # Edumacated guess based on the documentation
   # however untested so psycopg2 raises some other instead
   pass # or do_something_when_it_does_not_exist()
else:
   (do something)

3 Comments

So.. WhateverErrorPsypgRaises is the error message string? And the syntax is try: except: else: right?
well, except Exception is a bad habit and I don't have a postgres to test with to see what error is raised. I'll try to find it and improve my answer.
I added the try: except: else: to my code but it still gives me column "Tom" does not exist error.. Any idea about how to deal with it? Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.