2

Since the query returns more than 1 result, at the Get_results class how could i return the data_out as an array in order to iterate on the results of the query?

import psycopg2
import sys

class Get_results():
    def db_call(self,query,dbHost,dbName,dbUser,dbPass):
        try:
            con = None
            con = psycopg2.connect(host=dbHost, database=dbName,
                                   user=dbUser, password=dbPass)
            cur = con.cursor()
            cur.execute(query)
            data = cur.fetchall()
            for data_out in data:
                return data_out
        except psycopg2.DatabaseError, e:
                print 'Error %s' % e
                sys.exit(1)
        finally:
            if con:
                con.close()

 sql = " some sql "
 w = Get_results()
 for i in  w.db_call(sql, dbHost, dbName, dbUser, dbPass):
     print "The result is : " + i

For aditional info, when if i add print data right after data = cur.fetchall() i have the result:

[('The_Galaxy', 'The_Galaxy:star'),
 ('The_Galaxy', 'The_Galaxy:planet')]

2 Answers 2

4

The immediate answer is to change:

for data_out in data:
    data_out result

to:

for data_out in data:
    yield data_out

But you should look at using a with statement (if the DB API supports it), and simplifying the code - this could just be done by making a generator function (a class is OTT for this)

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

2 Comments

Hi, there was a mistake on the code, i updated the line data_out result to return data_out
@Thales Right - but either yield each row, or just return the cursor and iterate over that...
3
import psycopg2
import sys

class Get_results():
    def db_call(self,query,dbHost,dbName,dbUser,dbPass):
        try:
            con = None
            con = psycopg2.connect(host=dbHost, database=dbName,
                                   user=dbUser, password=dbPass)
            cur = con.cursor()
            cur.execute(query)
            data = cur.fetchall()  
            resultList = []  
            for data_out in data:  
                resultList.append(data_out[1])  


            return resultList   
        except psycopg2.DatabaseError, e:
                print 'Error %s' % e
                sys.exit(1)
        finally:
            if con:
                con.close()

 sql = " some sql "
 w = Get_results()
 for i in  w.db_call(sql, dbHost, dbName, dbUser, dbPass):
     print "The result is : " + i

5 Comments

this will print the value of column with index 0. To print other columns just change the index value in the return statement i.e. resultList.append(data_out[n])
I tried your solution, but im still receiving one result only.
Hi, the sql is simple , is "select * from table" it returns only 2 values.
is it printing The_Galaxy twice in the result? if so use resultList.append(data_out[1]) and check the output.
Prashant, really sorry, the code you described works. the failure was mine for inserting the return resultList inside the for data_out loop. Thanks a lot :)

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.