1

I have written a web.py service in python to access PostGres and get the table names inside the specific database.

CODE:

 def GET(self,r):
          web.header('Access-Control-Allow-Origin',      '*')
          web.header('Access-Control-Allow-Credentials', 'true')
          tables = []
          datasetID = web.input().dataSetID
          cursor = conn.cursor()
          cursor.execute("select relname from pg_class where relkind='r' and relname !~ '^(pg_|sql_)';")
          tablesWithDetails =    cursor.fetchall()
          print tablesWithDetails
          for x in tablesWithDetails:
            x.replace("(", "")
            x.replace(")","")
            tables.append(x)
            print tables;

This prints the table as follows,

[('acam_datasegregationdetails',), ('acam_datasegregationheader',), ('idn_accessinformation',), ('idn_b2cuseraccountmapping',), ('idn_b2cuserdevicemapping',), ('idn_b2cusers',), ('idn_roles',), ('idn_useraccountmapping')]

Needed Output:

['acam_datasegregationdetails', 'acam_datasegregationheader', idn_accessinformation', 'idn_b2cuseraccountmapping', 'idn_b2cuserdevicemapping', 'idn_b2cusers', 'idn_roles', 'idn_useraccountmapping']
6
  • 3
    try this tables.append(x[0]) if that does not work can you type type(x) Commented Nov 23, 2015 at 10:48
  • do you know y it worked ? Commented Nov 23, 2015 at 10:50
  • it takes the 0th element right? Commented Nov 23, 2015 at 10:51
  • 2
    the result from cursor.fetchall() is a List of Tuple when you iterate over it you get tuples which is in your case x which is a one element tuple. Now x[0] gives first element of the tuples Commented Nov 23, 2015 at 10:53
  • @VigneshKalai: Why don't post an answer? :) Commented Nov 23, 2015 at 10:56

2 Answers 2

7

Drop that loop and in instead do

tables = [t[0] for t in tablesWithDetails]

It will build a list containing the first element of each tuple in the result set.

Or even simpler (and cheaper), if you want a list then return an array which will be adapted to a list by Psycopg:

cursor.execute("""
    select array_agg(relname)
    from pg_class
    where relkind='r' and relname !~ '^(pg_|sql_)';"
""")
tables = cursor.fetchall()[0][0]
Sign up to request clarification or add additional context in comments.

9 Comments

Then how to run x.replace("(", "") and x.replace(")", "")?
@Kevin As I understand that is wrong. The OP was trying to "fix" or "cast" the tuples in the returned list supposing it could somehow be treated as a string.
Hmm...maybe. However tuples don't have .replace() method. Upvoted.
@KevinGuan that is what bothers be it should throw an error stating there is no .replace method in a tuple
@VigneshKalai: Do you mean about: Why doesn't OP's code raise an error about that .replace?
|
2

The problem is due this piece of code

tables.append(x)

When you execute cursor.fetchall() you will get a List of Tuples

and when you do for x in tablesWithDetails: you are iterating over the list by one tuple at a time

So when you do tables.append(x) you are appending a single element tuple to the list

To change that you could do this tables.append(x[0]) it appends the first element of the tuple

3 Comments

By the way, what about tables.extend(x)?
@KevinGuan it would certainly work at this condition but what if there are many element in the tuple then extend would append all the element
Yep, just think that use .extend would be more clear at this condition :P

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.