I am looking to create 26 separate sql databases, each with the same structure. (i.e. one for each letter of the alphabet), ideally which i can access from a dictionary or similar [i.e. to access the database corresponding to the letter a
with database["a"]
].
I currently have the following code, which generates one sql database (in this case for the letter a) using peewee.
from peewee import *
database_location_a = "C:\\database\\a.db"
data_sql_a= SqliteDatabase(database_location_a, threadlocals=True, pragmas=(("synchronous", "off"),))
class BaseModel(Model):
class Meta:
database = data_sql_a
class main_table(BaseModel):
file_name = CharField(primary_key = True)
year = CharField()
data_sql_a.connect()
data_sql_a.create_tables([main_table])
There are parts of this code i can easily loop over (e.g. i can easily create a dictionary of file locations). However, where i am stuck is that given the location is coded into the class Basemodel
, how do i loop over that [i.e. do i need 26 separate classes, and if so, can i create that without needing to copy/paste the class 26 times]? And similarly, given main_table
uses BaseModel
, would i need 26 separate instances of that class as well?
I may well be going about this the wrong way, but want to know what approach could i take to adapt this code to create multiple files, without needing to copy/paste it multiple times?
I was able to get a running solution using the peewee database proxy object.
from peewee import *
database_proxy = Proxy()
class BaseModel(Model):
class Meta:
database = database_proxy # Use proxy for our DB.
class main_table(BaseModel):
file_name = CharField(primary_key = True)
year = CharField()
import string
db_names = string.ascii_lowercase
# print(db_names) # abcdefghijklmnopqrstuvwxyz
'''
Loop over all the characters in the string and create databases in the current folder of this script
'''
for db_name in db_names:
database = SqliteDatabase('{}.db'.format(db_name), threadlocals=True, pragmas=(("synchronous", "off"),))
database_proxy.initialize(database)
database_proxy.connect()
database_proxy.create_tables([main_table])
According to the Proxy documentation, you can just use that database_proxy
variable as you normally would for the database
object after calling database_proxy.initialize(database)
. For example, the connect()
and create_tables()
are actually being called on database
through database_proxy
.