length power wrote: > When cur.execute be used, i get right output. > > import sqlite3 > con=sqlite3.connect(":memory:") > cur=con.cursor() > sql1="attach database 'g:\\workspace\\data\\Cinfo.sqlite' as Cinfo;" > sql2="select * from Cinfo.ipo;" > cur.execute(sql1) > cur.execute(sql2) > con.commit() > x=cur.fetchall()print(x) > > When i change it into cur.executescript, nothing can get. > > import sqlite3 > con=sqlite3.connect(":memory:") > cur=con.cursor() > sql_script=""" > attach database 'g:\\workspace\\data\\Cinfo.sqlite' as Cinfo; > select * from Cinfo.ipo;""" > cur.executescript(sql_script) > con.commit() > x=cur.fetchall()print(x) > > I want to know why?
The difference has nothing to do with your specific code, as the following minimal example shows: >>> import sqlite3 >>> cs = sqlite3.connect(":memory:").cursor() >>> cs.execute("select 42;").fetchall() [(42,)] >>> cs.executescript("select 42;").fetchall() [] The docs don't say so explicitly, but my guess is that executescript() is meant to execute sql statements for their side effect (like populating a database) only, not to return a result. Please file a bug report if you want a clarification in the docs. -- https://mail.python.org/mailman/listinfo/python-list