-
-
Notifications
You must be signed in to change notification settings - Fork 118
Mechanism to make memory DB created by sqlite-utils memory
available to other commands
#643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Labels
Comments
Documented a pattern using this here: https://github.com/simonw/sqlite-utils/blob/8906f57740aaf3e9ea77af3de0b061a64a2f5d3b/docs/plugins.rst#register_commandscli from contextlib import redirect_stdout
import io
@cli.command()
@click.pass_context
@click.argument(
"paths",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True),
required=False,
nargs=-1,
)
def show_schema_for_files(ctx, paths):
from sqlite_utils.cli import memory
with redirect_stdout(io.StringIO()):
ctx.invoke(memory, paths=paths, sql="select 1")
db = sqlite_utils.Database(memory_name="sqlite_utils_memory")
# Now do something with that database
click.echo(db.schema) |
OK, I massively over-engineered this. There's a much neater pattern: from sqlite_utils.cli import memory
db = ctx.invoke(memory, paths=paths, return_db=True) |
sqlite-utils memory
available to other commandssqlite-utils memory
available to other commands
Much neater: @cli.command()
@click.pass_context
@click.argument(
"paths",
type=click.Path(file_okay=True, dir_okay=False, allow_dash=True),
required=False,
nargs=-1,
)
def show_schema_for_files(ctx, paths):
from sqlite_utils.cli import memory
db = ctx.invoke(memory, paths=paths, return_db=True)
# Now do something with that database
click.echo(db.schema) |
simonw
added a commit
that referenced
this issue
Nov 8, 2024
simonw
added a commit
to simonw/sqlite-utils-ask
that referenced
this issue
Nov 8, 2024
simonw
added a commit
that referenced
this issue
Nov 23, 2024
simonw
added a commit
to simonw/sqlite-utils-ask
that referenced
this issue
Nov 24, 2024
Closes #10. Uses simonw/sqlite-utils#643 Depend on sqlite-utils>=3.38
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For an experimental feature for https://github.com/simonw/sqlite-utils-ask I found myself wanting to invoke the existing
sqlite-utils memory
command and then do something else with the memory database it created.I can do this with a
sqlite_utils.Database(memory_name="x")
but I need to ensure the variable doesn't go out of scope or it will be dropped by thesqlite3
library.The text was updated successfully, but these errors were encountered: