Helper that wraps execution of SQL code. This is used both by the REPL and by direct execution from the CLI. 'c' may be a cursor or a connection. 'sql' is the SQL string to execute.
(c, sql, suppress_errors=True)
| 13 | |
| 14 | |
| 15 | def execute(c, sql, suppress_errors=True): |
| 16 | """Helper that wraps execution of SQL code. |
| 17 | |
| 18 | This is used both by the REPL and by direct execution from the CLI. |
| 19 | |
| 20 | 'c' may be a cursor or a connection. |
| 21 | 'sql' is the SQL string to execute. |
| 22 | """ |
| 23 | |
| 24 | try: |
| 25 | for row in c.execute(sql): |
| 26 | print(row) |
| 27 | except sqlite3.Error as e: |
| 28 | tp = type(e).__name__ |
| 29 | try: |
| 30 | print(f"{tp} ({e.sqlite_errorname}): {e}", file=sys.stderr) |
| 31 | except AttributeError: |
| 32 | print(f"{tp}: {e}", file=sys.stderr) |
| 33 | if not suppress_errors: |
| 34 | sys.exit(1) |
| 35 | |
| 36 | |
| 37 | class SqliteInteractiveConsole(InteractiveConsole): |