2

I want to create multiple in-memory sqlite databases with names in python.

In sqlite command line it's possible to do that with this syntax:

':memory:' AS database_name

but when using it in python conenction strings like this:

con = sqlite3.connect("':memory:' AS database_name")

get error.

My problem is how can we create multiple in memory databases with names for accessing them.

How we could do that in python?

1 Answer 1

6

The AS clause works only with the ATTACH statement, but not for the main database (which is always named main).

And the identify of a database comes from its file name, so changing the database name would not help anyway.

To use a custom "file" name for an in-memory database, use a URI file name with the mode parameter:

conn = sqlite3.connect("file:blah?mode=memory", uri=True)

But to attach another in-memory database with a custom database name to the same connection, just execute ATTACH normally:

conn.execute("ATTACH ':memory:' AS db2")
Sign up to request clarification or add additional context in comments.

1 Comment

would you show me an example that is needed for my purpose?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.