python - Creating multiple sql databases of the same structure using Peewee -
i looking create 26 separate sql databases, each same structure. (i.e. 1 each letter of alphabet), ideally can access dictionary or similar [i.e. access database corresponding letter a
database["a"]
].
i have following code, generates 1 sql database (in case 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 parts of code can loop on (e.g. can create dictionary of file locations). however, stuck given location coded class basemodel
, how loop on [i.e. need 26 separate classes, , if so, can create without needing copy/paste class 26 times]? , similarly, given main_table
uses basemodel
, need 26 separate instances of class well?
i may going wrong way, want know approach take adapt code create multiple files, without needing copy/paste multiple times?
i able running solution using peewee database proxy object.
from peewee import * database_proxy = proxy() class basemodel(model): class meta: database = database_proxy # use proxy 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 on characters in string , create databases in current folder of script ''' 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 proxy documentation, can use database_proxy
variable database
object after calling database_proxy.initialize(database)
. example, connect()
, create_tables()
being called on database
through database_proxy
.
Comments
Post a Comment