Database Tools¶
These tools will initialize the database and provide connections to the database for querying the database.
SQLite Database¶
-
gsshapy.lib.db_tools.
init_sqlite_db
(path, initTime=False)[source]¶ Initialize SQLite Database
Parameters: - path (str) – Path to database (Ex. ‘/home/username/my_sqlite.db’).
- initTime (Optional[bool]) – If True, it will print the amount of time to generate database.
Example:
from gsshapy.lib.db_tools import init_sqlite_db, create_session sqlite_db_path = '/home/username/my_sqlite.db' init_postgresql_db(path=sqlite_db_path) sqlalchemy_url = init_sqlite_db(path=sqlite_db_path) db_work_sessionmaker = get_sessionmaker(sqlalchemy_url) db_work_session = db_work_sessionmaker() ##DO WORK db_work_session.close()
-
gsshapy.lib.db_tools.
init_sqlite_memory
(initTime=False)[source]¶ Initialize SQLite in Memory Only Database
Parameters: initTime (Optional[bool]) – If True, it will print the amount of time to generate database. Returns: The tuple contains sqlalchemy_url(str), which is the path to use when creating a session as well as engine(str), which is the path to use when creating a session. Return type: tuple Example:
from gsshapy.lib.db_tools import init_sqlite_memory, create_session sqlalchemy_url, engine = init_sqlite_memory() db_work_sessionmaker = get_sessionmaker(sqlalchemy_url, engine) db_work_session = db_work_sessionmaker() ##DO WORK db_work_session.close()
PostgreSQL Database¶
-
gsshapy.lib.db_tools.
init_postgresql_db
(username, host, database, port='', password='', initTime=False)[source]¶ Initialize PostgreSQL Database
Note
psycopg2 or similar driver required
Parameters: - username (str) – Database username.
- host (str) – Database host URL.
- database (str) – Database name.
- port (Optional[int,str]) – Database port.
- password (Optional[str]) – Database password.
- initTime (Optional[bool]) – If True, it will print the amount of time to generate database.
Example:
from gsshapy.lib.db_tools import init_postgresql_db, create_session sqlalchemy_url = init_postgresql_db(username='gsshapy', host='localhost', database='gsshapy_mysql_tutorial', port='5432', password='pass') db_work_sessionmaker = get_sessionmaker(sqlalchemy_url) db_work_session = db_work_sessionmaker() ##DO WORK db_work_session.close()
MySQL Database¶
-
gsshapy.lib.db_tools.
init_mysql_db
(username, host, database, port='', password='', initTime=False)[source]¶ Initialize MySQL Database
Note
mysql-python or similar driver required
Parameters: - username (str) – Database username.
- host (str) – Database host URL.
- database (str) – Database name.
- port (Optional[int,str]) – Database port.
- password (Optional[str]) – Database password.
- initTime (Optional[bool]) – If True, it will print the amount of time to generate database.
Example:
from gsshapy.lib.db_tools import init_mysql_db, create_session sqlalchemy_url = init_mysql_db(username='gsshapy', host='localhost', database='gsshapy_mysql_tutorial', port='5432', password='pass') db_work_sessionmaker = get_sessionmaker(sqlalchemy_url) db_work_session = db_work_sessionmaker() ##DO WORK db_work_session.close()