mercredi 17 juin 2015

Python Sqlite3: Create a schema without having to use a second database

I would like to create a schema for a sqlite3 database in python, but sqlite does not appear to support CREATE SCHEMA (sqlite docs). I've looked into ATTACH, and it seems like it would do the job by using a second database but I only need one database that has a schema.

I would like to do something along these lines:

import sqlite3
db = sqlite3.connect('db_file.db')
db.execute("CREATE TABLE my_schema.my_table(column TYPE);")
db.commit()

However, it throws an exception:

Traceback (most recent call last):
  File "C:/Users/Mod/Projects/sandbox/test_db.py", line 8, in <module>
    db.execute("CREATE TABLE my_schema.my_table(column TYPE);")
OperationalError: unknown database my_schema

I know I can use ATTACH like so...

import sqlite3
db = sqlite3.connect('db_file.db')
db.execute("ATTACH DATABASE 'db_file_2.db' AS 'my_schema';")
db.execute("CREATE TABLE my_schema.my_table(column TYPE);")
db.commit()

...but is there any way to create a schema without a second database?

Aucun commentaire:

Enregistrer un commentaire