From f67fa9c898a4713850e16934046f0fe2cba8c44c Mon Sep 17 00:00:00 2001 From: Gerhard Haering Date: Sun, 4 Jan 2015 01:49:24 +0100 Subject: [PATCH] Fix https://github.com/ghaering/pysqlite/issues/69 Instead of the proposed patch, I decided to remove "cleverness" by not relying on the detected statement type here. Instead, a cursor.description is built if sqlite says that if has information about the rows. --- lib/test/types.py | 16 ++++++++++++++++ src/cursor.c | 10 +++++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/test/types.py b/lib/test/types.py index 73d22ec..a4ff091 100644 --- a/Lib/sqlite3/test/types.py +++ b/Lib/sqlite3/test/types.py @@ -329,6 +329,22 @@ def CheckCursorDescriptionNoRow(self): self.cur.execute("select * from test where 0 = 1") self.assert_(self.cur.description[0][0] == "x") + def CheckCursorDescriptionInsert(self): + self.cur.execute("insert into test values (1)") + self.assert_(self.cur.description is None) + + def CheckCursorDescriptionCTE(self): + if sqlite.sqlite_version_info < (3, 8, 3): + # Common Table Expressions not supported + return + + self.cur.execute("insert into test values (1)") + self.cur.execute("insert into test values (1)") + self.cur.execute("with bar as (select sum(x) as colname from test) select * from bar") + self.assert_(self.cur.description is not None) + self.assertEqual(self.cur.description[0][0], "colname") + + class ObjectAdaptationTests(unittest.TestCase): def cast(obj): return float(obj) diff --git a/src/cursor.c b/src/cursor.c index 284b602..f1651b9 100644 --- a/Modules/_sqlite/cursor.c +++ b/Modules/_sqlite/cursor.c @@ -692,12 +692,12 @@ PyObject* _pysqlite_query_execute(pysqlite_Cursor* self, int multiple, PyObject* goto error; } - if (rc == SQLITE_ROW || (rc == SQLITE_DONE && statement_type == STATEMENT_SELECT)) { - if (self->description == Py_None) { - Py_BEGIN_ALLOW_THREADS - numcols = sqlite3_column_count(self->statement->st); - Py_END_ALLOW_THREADS + if (rc == SQLITE_ROW || (rc == SQLITE_DONE)) { + Py_BEGIN_ALLOW_THREADS + numcols = sqlite3_column_count(self->statement->st); + Py_END_ALLOW_THREADS + if (self->description == Py_None && numcols > 0) { Py_DECREF(self->description); self->description = PyTuple_New(numcols); if (!self->description) {