Aug-10-2017, 05:27 PM
My question concerns the behavior of SQLite3 with query.
When a select query has been executed, cursor attribute description provides the list of column names of the result set. And this, whether this result set is empty or not (as told in the documentation: It is set for SELECT statements without any matching rows as well).
Query with-select behaves exactly like select, except when the result set is empty, in which case cursor attribute description is set to None, which is particularly disturbing.
Versions: Windows 7, Python 2.7.12, SQLite 3.17
The following Python program
Has anybody observed it?
Is there another way to get column names?
Thanks
When a select query has been executed, cursor attribute description provides the list of column names of the result set. And this, whether this result set is empty or not (as told in the documentation: It is set for SELECT statements without any matching rows as well).
Query with-select behaves exactly like select, except when the result set is empty, in which case cursor attribute description is set to None, which is particularly disturbing.
Versions: Windows 7, Python 2.7.12, SQLite 3.17
The following Python program
- opens an in-memory DB, creates table T(A,B) in which it stores three data rows.
- creates a with-select query that selects the rows of T where B = <some value>
- executes the query with value 4
- displays the result set
- displays the names of the columns
import sqlite3 conn = sqlite3.connect(u':memory:') c = conn.cursor() c.execute(u'create table T(A integer, B integer)') c.execute(u'insert into T values (1,2),(2,4),(3,6)') query = u'with CTE(X,Y) as (select A,B from T where B = ?) select X,Y from CTE' c.execute(query,(4,)) for r in c: print r for d in c.description: print d c.close() conn.close()The result is as expected:
Output:('X', None, None, None, None, None, None)
('Y', None, None, None, None, None, None)
(2, 4)
However, if I change constant 4 for 5, the result set is empty so that nothing is displayed, then the program crashes:Error:Traceback (most recent call last):
File "C:\Users\Administrateur\Desktop\Python_Test_With.py", line 17, in <module>
for d in c.description:
TypeError: 'NoneType' object is not iterable
I have found no trace of this problem on the web. Has anybody observed it?
Is there another way to get column names?
Thanks