Python Forum
How to execute a string with multiple sqlite statements and print results?
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to execute a string with multiple sqlite statements and print results?
#1
Hello,

How can a string with multiple sqlite statements be executed (for example "select * from tab1; select * from tab2") and print the results?
Python 3.5.3 (default, Sep 27 2018, 17:25:39) 
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sqlite3
>>> conn = sqlite3.connect('sqlfun.db')
>>> c = conn.cursor()
>>> c.execute("select * from tab1")
<sqlite3.Cursor object at 0x7f7462ce0960>
>>> c.fetchall()
[(1,), (2,)]
>>> c.execute("select * from tab2")
<sqlite3.Cursor object at 0x7f7462ce0960>
>>> c.fetchall()
[(3,), (4,)]
>>> c.executescript("select * from tab1; select * from tab2")
<sqlite3.Cursor object at 0x7f7462ce0960>
>>> c.fetchall()
[]
compare this with sqlite3 -
SSQLite version 3.25.3 2018-11-05 20:37:38
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> .open sqlfun.db
sqlite> select * from tab1;
1
2
sqlite> select * from tab2;
3
4
sqlite> select * from tab1; select * from tab2;
1
2
3
4
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  splitting file into multiple files by searching for string AlphaInc 2 890 Jul-01-2023, 10:35 PM
Last Post: Pedroski55
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,369 Sep-27-2022, 01:38 PM
Last Post: buran
  Remove a space between a string and variable in print sie 5 1,780 Jul-27-2022, 02:36 PM
Last Post: deanhystad
  print statements without () petec 7 1,994 Jun-17-2022, 02:27 AM
Last Post: snippsat
  Matching multiple parts in string fozz 31 6,251 Jun-13-2022, 09:38 AM
Last Post: fozz
  Multiple Loop Statements in a Variable Dexty 1 1,203 May-23-2022, 08:53 AM
Last Post: bowlofred
  Can you print a string variable to printer hammer 2 1,944 Apr-30-2022, 11:48 PM
Last Post: hammer
  Search multiple CSV files for a string or strings cubangt 7 8,006 Feb-23-2022, 12:53 AM
Last Post: Pedroski55
  Print first day of the week as string in date format MyerzzD 2 2,012 Sep-29-2021, 06:43 AM
Last Post: MyerzzD
  Replace String in multiple text-files [SOLVED] AlphaInc 5 8,103 Aug-08-2021, 04:59 PM
Last Post: Axel_Erfurt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020