diff -r 3fffe9681f60 Modules/_sqlite/cursor.c --- a/Modules/_sqlite/cursor.c Mon Jun 23 15:14:50 2014 +0200 +++ b/Modules/_sqlite/cursor.c Tue Jun 24 11:50:48 2014 +0200 @@ -612,10 +612,15 @@ PyObject* _pysqlite_query_execute(pysqli while (1) { /* Actually execute the SQL statement. */ rc = pysqlite_step(self->statement->st, self->connection); + if (PyErr_Occurred()) { + (void)pysqlite_statement_reset(self->statement); + goto error; + } if (rc == SQLITE_DONE || rc == SQLITE_ROW) { /* If it worked, let's get out of the loop */ break; } + /* Something went wrong. Re-set the statement and try again. */ rc = pysqlite_statement_reset(self->statement); if (rc == SQLITE_SCHEMA) { @@ -685,6 +690,9 @@ PyObject* _pysqlite_query_execute(pysqli } self->next_row = _pysqlite_fetch_one_row(self); + if (self->next_row == NULL && PyErr_Occurred()) { + goto error; + } } else if (rc == SQLITE_DONE && !multiple) { pysqlite_statement_reset(self->statement); Py_CLEAR(self->statement); @@ -807,7 +815,10 @@ PyObject* pysqlite_cursor_executescript( rc = SQLITE_ROW; while (rc == SQLITE_ROW) { rc = pysqlite_step(statement, self->connection); - /* TODO: we probably need more error handling here */ + if (PyErr_Occurred()) { + (void)sqlite3_finalize(statement); + goto error; + } } if (rc != SQLITE_DONE) { @@ -884,6 +895,11 @@ PyObject* pysqlite_cursor_iternext(pysql if (self->statement) { rc = pysqlite_step(self->statement->st, self->connection); + if (PyErr_Occurred()) { + (void)pysqlite_statement_reset(self->statement); + Py_DECREF(next_row); + return NULL; + } if (rc != SQLITE_DONE && rc != SQLITE_ROW) { (void)pysqlite_statement_reset(self->statement); Py_DECREF(next_row);