views:

162

answers:

4

I am trying to write a simple query to an sqlite database in a python script. To test if my parameters were correct, I tried running the query from the ipython command line. It looked something like this:

import sqlite3
db = 'G:\path\to\db\file.sqlite'
conn = sqlite3.connect(db)
results = conn.execute('SELECT * FROM studies').fetchall()

for some reason, my results came back totally empty. Then I tried another test query:

results = conn.execute('SELECT id FROM studies').fetchall()

Which returned correctly. I figured there was a problem with the asterisk [WRONG, SEE SECOND UPDATE BELOW], so I tried the 'SELECT * FROM studies' query from a default python command line. Lo and behold, it returned correctly. I tried all the normal ways to escape the asterisk only to be met by a wide variety of error messages. Is there any way to run this query in IPython?


EDIT: Sorry, I incorrectly assumed IronPython and IPython were the same. What I meant was the IPython command line, not the IronPython framework.


EDIT2: Okay, it turns out the asterisk DOES work as shown by this successful query:

'SELECT COUNT(*) FROM studies'

From the suggestions posted here, it turns out the error results from trying to return records with multiple fields, i.e.:

'SELECT field1,field2 FROM studies'

which still results in to records being returned. I have changed the title of the question accordingly.

+1  A: 

This is SQL. IronPython has little or nothing to do with the processing of the query. Are you using an unusual character encoding? (IE not UTF-8 or ASCII)?

What happens if you SELECT id,fieldname,fieldname FROM studies (In other words, simulating what '*' does.)

Christopher
Simulating the '*' query also returned nothing. It seems that the query only works when I ask for one field. I tried the query you suggested in the regular python command line, and I got the correct answer. This leads me to believe that ironpython is doing something a little differently before the sql query is sent.
Francisco
If you just run the query at the interpreter prompt, what does it say the return value is?
Christopher
A: 

Just a wild guess, but please try to escape backslashes in the path to the database file. In other words instead of

db = 'G:\path\to\db\file.sqlite'

try

db = 'G:\\path\\to\\db\\file.sqlite'
Adam Byrtek
Nope, no difference.
Francisco
+1  A: 

Some more debugging you could try:

s = 'SELEECT * from studies'
print s
conn.execute(s).fetchall()

or:

s = 'SELECT ' + chr(42) + ' from studies'
conn.execute(s).fetchall()

You might also try:

conn.execute('select count(*) from studies').fetchall()

if that comes back as [(0,)] then something really weird is going on :-)


Some more things you could try:

conn.execute('select id from (select * from studies)').fetchall()

or:

cur = conn.cursor()
cur.execute('select * from studies').fetchall()
John Fouhy
Your first two suggestions return zero results. :('select count(*) from studies' returns the correct amount of rows, so the asterisk character still works correctly. For now, the error only happens with I try to return results with more than one field. i.e.: 'SELECT field1,field2 FROM studies'
Francisco
+1  A: 

I've tried all the things you've mentioned in IPython and sqlite without any problems (ipython 0.9.1, python 2.5.2).

Is there a chance this is some kind of version mismatch issue? Maybe your shells are referencing different libraries?

For example, does

import sqlite3; print sqlite3.version

return the same thing from both shells (i.e. ipython and the regular one where the sql query works)?

How about

conn.execute('select sqlite_version()').fetchall()

Does that return the same thing?

ars
My version of ipython is also 0.9.1, but I'm using python 2.6.2. Both shells had matching version for both of questions you asked. Specifically, 'sqlite3.version' returned 2.4.1, and 'SELECT sqlite_version()' returned 3.5.9
Francisco