create database

Core database functionality for the keybindings_fps app. This module is meant to run once to create the database and tables. Don’t run this module again if the database already exists, because it will drop the existing tables.

Initialize database


source

init_db

 init_db (data_dir:pathlib.Path=None)

Initialize the database connection Args: data_dir: Optional path to data directory. If None, uses project’s data dir

db = init_db()
db.t.actions()[0].keys()
dict_keys(['id', 'name', 'description', 'category_id'])

Create the tables


source

create_tables

 create_tables (db:<function database>, overwrite_existing:bool=False)

Create all required database tables.

Type Default Details
db database Database connection
overwrite_existing bool False Remove all existing data in database

Open existing database in project data directory

db = init_db()

Check that the database is opened in the correct location

db.conn.filename
'/home/jelle/code/keybindings_fps/data/game_bindings.db'

Modify structure of database tables

Add a new column to an existing table


source

add_clmn_to_table

 add_clmn_to_table (db, table, column, col_type, **kwargs)

Add a new column to an existing table

db.t['bindings'].c
action_id, description, game_id, id, key_id, modifier_id, sort_order
add_clmn_to_table(db, 'bindings', 'test', str, not_null_default='test text')
<Table bindings (id, game_id, action_id, key_id, modifier_id, description, sort_order, test)>
test_eq(db.t['bindings'].columns[-1].name, 'test')

Delete/drop a column from an existing table


source

drop_clmn_from_table

 drop_clmn_from_table (db, table, column)

Drop a column from an existing table

drop_clmn_from_table(db, 'bindings', 'test')
<Table bindings (id, game_id, action_id, key_id, modifier_id, description, sort_order)>
test_eq('test' not in db.t['bindings'].c, True)
db.conn.close()