0

This is a minimum code:

import sqlite3 as sq3
import os
import sys

def main(argv):
    q = 'select * from table_name;'
    db = 'test.db'

    con = sq3.connect(db)
    cur = con.cursor()
    cur.executescript(q)    // cur.execute(q) will work

    print cur.fetchone()


if __name__ == '__main__':
    sys.exit(main(sys.argv))

My problem is executescript always fails while execute works fine. Is it because executescript is Nonstandard or some libraries I missed?

2 Answers 2

4

executescript isn't supposed to return anything, what would it return? The last statement? The first statement? or maybe that one in the middle.

Since it allows you to execute multiple SQL statements there is no way to tell which one you want to have returned.

Sign up to request clarification or add additional context in comments.

5 Comments

Sorry for misleading, I should have said that cur.fetchone() is None.
Yes, executescript does not save any results in the cursor, thus fetchone won't have anything to fetch. There is no way for the sql library to know what you want returned, you should use execute instead.
executescript does not affect the cursor!!! That makes sense!!! Why should it affect the cursor!!! silly mistake. Thanks.
@Wessie if you can point to a reliable source saying that executescript does not save any results in cursor, that would be much appreciated. Because my understanding is that executescript just separates into a bunch of statements, executes each one, and commits. If that is the case (I have to test), then the last statement in the script could put data in the cursor (if it is a select statement), or perhaps even whatever last SELECT statement (even if followed by inserts). It would be nice if "no results in cursor were a 100% sure thing for scripts.
You might want the last statement returned, no?
2

executescript() is for executing multiple SQL commands, i.e., a script. What is the return value of multiple SQL commands? Hard to say, which is why executescript() returns None. You're not doing anything wrong nor do you have anything missing in your installation.

3 Comments

Sorry, corrected. What I meant is after executescript, cur.fetchone returns None.
Same basic reason. How is executescript() supposed to know which of the result sets produced by queries in the script should be made accessible via the cursor?
Thanks. That makes sense. Silly logic mistake.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.