Database Documentation
create_engine_db(db_path='sqlite:///DB.db')
Creates an SQLAlchemy Engine instance which is the starting point for any SQLAlchemy application.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
db_path |
str
|
The database URL to connect to. Defaults to an SQLite database named 'DB.db'. |
'sqlite:///DB.db'
|
Returns:
Type | Description |
---|---|
Engine
|
sqlalchemy.engine.base.Engine: A database engine that can be used to interact with the database. |
Source code in CRR/db/db.py
63 64 65 66 67 68 69 70 71 72 73 74 75 |
|
get_session(engine)
Creates and returns a new SQLAlchemy session bound to the given engine.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
engine |
Engine
|
The Engine instance connected to the database. |
required |
Returns:
Type | Description |
---|---|
Session
|
sqlalchemy.orm.session.Session: A session object that acts as a handle to the database. |
Source code in CRR/db/db.py
91 92 93 94 95 96 97 98 99 100 101 102 |
|
initialize_database(engine)
Creates all tables in the database defined in the Base metadata class using the provided engine.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
engine |
Engine
|
The Engine instance connected to the database. |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in CRR/db/db.py
77 78 79 80 81 82 83 84 85 86 87 |
|
push_data_to_db(session, df, table_class)
Inserts data from a DataFrame into the specified table in the database, matching DataFrame columns to table columns.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
session |
Session
|
The SQLAlchemy session to be used for database transactions. |
required |
df |
DataFrame
|
The DataFrame containing the data to be inserted into the database. |
required |
table_class |
type
|
The SQLAlchemy table class (model) that defines the schema of the table where data will be inserted. |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Description
The function filters the DataFrame to include only valid columns that exist in the database table. It handles missing values, converting them to None, and ensures that data types are correctly cast to match the column definitions in the database schema.
Source code in CRR/db/db.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
|
view_table(session, table_class)
Retrieves and prints all records from a specified table in the database.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
session |
Session
|
The SQLAlchemy session used for querying the database. |
required |
table_class |
type
|
The SQLAlchemy table class (model) from which records will be retrieved. |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Description
This function queries all instances of the specified table class and prints their attributes. This is useful for debugging or for simple inspection tasks where direct database access is impractical.
Source code in CRR/db/db.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
|