0

I created a table with 4 records. The table description is like: Employer

Now when I run the following piece of code:

import cx_Oracle
con = cx_Oracle.connect("system/******@localhost/xe")
cur = con.cursor()
cur.execute("SELECT * FROM Employer")
print(cur.fetchall())
print("\nNumber of rows fetched =",cur.rowcount)
print("\nDescription:",cur.description)`

The fetchall() fuction returns []. The rowcount is 0. But the description is correct, i.e. what it should be.

Description: [('COMPANYID', , 5, 5, 0, 0, 0), ('COMPANYNAME', , 50, 50, 0, 0, 0), ('EMAILID', , 30, 30, 0, 0, 1), ('MOBILE', , 11, 22, 10, 0, 1), ('CITY', , 15, 15, 0, 0, 1), ('INDUSTRYTYPE', , 20, 20, 0, 0, 1), ('FUNCTIONALAREA', , 20, 20, 0, 0, 1), ('MEMBERSHIPPLAN', , 20, 20, 0, 0, 1), ('DATEOFSIGNUP', , 23, 7, 0, 0, 1), ('DATEOFRENEWAL', , 23, 7, 0, 0, 1), ('RENEWALSTATUS', , 10, 10, 0, 0, 1)]

When I'm running the same query on the DB it runs fine. But here the fetchall() returns an empty tuple and the rowcount is 0. Please help. Thank you.

AliZaidi
  • 27
  • 1
  • 10
  • Are you sure you have your db selected. Try modifying you sql to show all tables or something else simple to actually make sure you are where you think you are – Joe Jul 08 '17 at 18:40
  • This is what happens when I run the query on my DB. http://imgur.com/YX75dkk @Joe – AliZaidi Jul 08 '17 at 18:46
  • Yeah I understand that. But try running a simpler query from the script to ensure that you are really connected to the db where you want to be. – Joe Jul 08 '17 at 18:57
  • What can be more simpler than a "select *" statement that too without a "where" clause? I'm surely connected to the DB and I am "where I want to be". If there was no connection I would have had an error in the first place. I ran a "delete" query from the script and it worked just fine. I think there must be something wrong in the way I'm writing the fetchall() function. But it looks fine to me. @Joe – AliZaidi Jul 08 '17 at 19:07
  • What you should try running is any command that you can get results from to ensure you are connected to what you think you are. Like the user below mentioned in his answer I believe that you are not actually making a good connection to the db. Or you are not selecting the right db to query – Joe Jul 08 '17 at 22:51
  • Turns out the service name in the connection string I was using was wrong. The issue is now solved. Thank you for your time. @Joe – AliZaidi Jul 09 '17 at 06:52

1 Answers1

0

Believe it or not, you are not connected to the correct database and/or the table is truly empty. The description you get is inferred by the database and does not require the query to have any results.

user2722968
  • 13,636
  • 2
  • 46
  • 67
  • Turns out the service name in the connection string I was using was wrong. The issue is now solved. Thank you for your time. @user2722968 – AliZaidi Jul 09 '17 at 06:52