Mike Bayer avatar Mike Bayer committed da2d958

- Fixed SQLite reflection methods so that non-present
cursor.description, which triggers an auto-cursor
close, will be detected so that no results doesn't
fail on recent versions of pysqlite which raise
an error when fetchone() called with no rows present.

Comments (0)

Files changed (2)

       creates an arbitrarily large number of engines
       or dialects.   There is a small performance penalty
       which will be resolved in 0.6.  [ticket:1299]
+
+-sqlite
+    - Fixed SQLite reflection methods so that non-present
+      cursor.description, which triggers an auto-cursor
+      close, will be detected so that no results doesn't
+      fail on recent versions of pysqlite which raise 
+      an error when fetchone() called with no rows present.
       
 - postgres
     - Added Index reflection support to Postgres, using a

lib/sqlalchemy/databases/sqlite.py

         else:
             pragma = "PRAGMA "
         qtable = quote(table_name)
-        cursor = connection.execute("%stable_info(%s)" % (pragma, qtable))
+        cursor = _pragma_cursor(connection.execute("%stable_info(%s)" % (pragma, qtable)))
+            
         row = cursor.fetchone()
 
         # consume remaining rows, to work around
             pragma = "PRAGMA %s." % preparer.quote_identifier(table.schema)
         qtable = preparer.format_table(table, False)
 
-        c = connection.execute("%stable_info(%s)" % (pragma, qtable))
+        c = _pragma_cursor(connection.execute("%stable_info(%s)" % (pragma, qtable)))
         found_table = False
         while True:
             row = c.fetchone()
         if not found_table:
             raise exceptions.NoSuchTableError(table.name)
 
-        c = connection.execute("%sforeign_key_list(%s)" % (pragma, qtable))
+        c = _pragma_cursor(connection.execute("%sforeign_key_list(%s)" % (pragma, qtable)))
         fks = {}
         while True:
             row = c.fetchone()
         for name, value in fks.iteritems():
             table.append_constraint(schema.ForeignKeyConstraint(value[0], value[1]))
         # check for UNIQUE indexes
-        c = connection.execute("%sindex_list(%s)" % (pragma, qtable))
+        c = _pragma_cursor(connection.execute("%sindex_list(%s)" % (pragma, qtable)))
         unique_indexes = []
         while True:
             row = c.fetchone()
                     break
                 cols.append(row[2])
 
-
+def _pragma_cursor(cursor):
+    if cursor.closed or not cursor.cursor.description:
+        cursor._fetchone_impl = lambda: None
+    return cursor
+        
 class SQLiteCompiler(compiler.DefaultCompiler):
     functions = compiler.DefaultCompiler.functions.copy()
     functions.update (
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.