1 | n/a | # A minimal SQLite shell for experiments |
---|
2 | n/a | |
---|
3 | n/a | import sqlite3 |
---|
4 | n/a | |
---|
5 | n/a | con = sqlite3.connect(":memory:") |
---|
6 | n/a | con.isolation_level = None |
---|
7 | n/a | cur = con.cursor() |
---|
8 | n/a | |
---|
9 | n/a | buffer = "" |
---|
10 | n/a | |
---|
11 | n/a | print("Enter your SQL commands to execute in sqlite3.") |
---|
12 | n/a | print("Enter a blank line to exit.") |
---|
13 | n/a | |
---|
14 | n/a | while True: |
---|
15 | n/a | line = input() |
---|
16 | n/a | if line == "": |
---|
17 | n/a | break |
---|
18 | n/a | buffer += line |
---|
19 | n/a | if sqlite3.complete_statement(buffer): |
---|
20 | n/a | try: |
---|
21 | n/a | buffer = buffer.strip() |
---|
22 | n/a | cur.execute(buffer) |
---|
23 | n/a | |
---|
24 | n/a | if buffer.lstrip().upper().startswith("SELECT"): |
---|
25 | n/a | print(cur.fetchall()) |
---|
26 | n/a | except sqlite3.Error as e: |
---|
27 | n/a | print("An error occurred:", e.args[0]) |
---|
28 | n/a | buffer = "" |
---|
29 | n/a | |
---|
30 | n/a | con.close() |
---|