Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

return value of execute

Is the return value of sqlite3.execute always self? i.e. can I safely return some_connection_obj.execute(...) or use method chaining? I fail to find relevant text in that document.

like image 377
apple apple Avatar asked Nov 28 '25 04:11

apple apple


1 Answers

When documentation fails, look into the code (python 3.6.4):

At the _pysqlite_query_execute function end:

    if (PyErr_Occurred()) {
        self->rowcount = -1L;
        return NULL;
    } else {
        Py_INCREF(self);
        return (PyObject*)self;
    }
}

PyObject* pysqlite_cursor_execute(pysqlite_Cursor* self, PyObject* args)
{
    return _pysqlite_query_execute(self, 0, args);
}

So, unless an error occurred, the method returns self.

However, according to PEP 249 (for DB API 2) return values are not defined, which means you are on your own, and possibly the code you write in that way will not be portable among different DB API 2 implementations. And indeed, according to psycopg docs .execute returns None.

The answer is, one can't safely chain methods in a broader sense, because doing so limits future code reuse possibilities.

like image 116
Roman Susi Avatar answered Nov 30 '25 18:11

Roman Susi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!