(*args)
| 71 | |
| 72 | |
| 73 | def main(*args): |
| 74 | parser = ArgumentParser( |
| 75 | description="Python sqlite3 CLI", |
| 76 | color=True, |
| 77 | ) |
| 78 | parser.add_argument( |
| 79 | "filename", type=str, default=":memory:", nargs="?", |
| 80 | help=( |
| 81 | "SQLite database to open (defaults to ':memory:'). " |
| 82 | "A new database is created if the file does not previously exist." |
| 83 | ), |
| 84 | ) |
| 85 | parser.add_argument( |
| 86 | "sql", type=str, nargs="?", |
| 87 | help=( |
| 88 | "An SQL query to execute. " |
| 89 | "Any returned rows are printed to stdout." |
| 90 | ), |
| 91 | ) |
| 92 | parser.add_argument( |
| 93 | "-v", "--version", action="version", |
| 94 | version=f"SQLite version {sqlite3.sqlite_version}", |
| 95 | help="Print underlying SQLite library version", |
| 96 | ) |
| 97 | args = parser.parse_args(*args) |
| 98 | |
| 99 | if args.filename == ":memory:": |
| 100 | db_name = "a transient in-memory database" |
| 101 | else: |
| 102 | db_name = repr(args.filename) |
| 103 | |
| 104 | # Prepare REPL banner and prompts. |
| 105 | if sys.platform == "win32" and "idlelib.run" not in sys.modules: |
| 106 | eofkey = "CTRL-Z" |
| 107 | else: |
| 108 | eofkey = "CTRL-D" |
| 109 | banner = dedent(f""" |
| 110 | sqlite3 shell, running on SQLite version {sqlite3.sqlite_version} |
| 111 | Connected to {db_name} |
| 112 | |
| 113 | Each command will be run using execute() on the cursor. |
| 114 | Type ".help" for more information; type ".quit" or {eofkey} to quit. |
| 115 | """).strip() |
| 116 | sys.ps1 = "sqlite> " |
| 117 | sys.ps2 = " ... " |
| 118 | |
| 119 | con = sqlite3.connect(args.filename, isolation_level=None) |
| 120 | try: |
| 121 | if args.sql: |
| 122 | # SQL statement provided on the command-line; execute it directly. |
| 123 | execute(con, args.sql, suppress_errors=False) |
| 124 | else: |
| 125 | # No SQL provided; start the REPL. |
| 126 | console = SqliteInteractiveConsole(con) |
| 127 | try: |
| 128 | import readline # noqa: F401 |
| 129 | except ImportError: |
| 130 | pass |
no test coverage detected