Connect – Access the pklmart database

connect to the pklmart database

In practice, you can probably just ignore this module. pklshop includes the data from pklmart already loaded into pandas dataframes. Connect is only needed if you want to access the database directly and have permission. It is also used to update the dataframes in pklshop periodically.


source

config

 config (filename='../database.ini', section='postgresql')

Reads the database.ini file and returns the connection parameters as a dictionary. Assumes the ini file is in the parent directory

You will now want to set up a database.ini file. This will contain the sensitive information needed to connect to the databse. To avoid this information being public, be sure to add *.ini to your .gitignore. Once completed, it should look something like this:

database.ini

[postgresql]
database=DATABASE
host=HOST
user=USERNAME
password=PASSWORD
port=PORT

where you replace the uppercase values with the appropriate information.

We can now use the config function to get the appropriate information:

params = config()

We can now connect to the database using a DbConnection object


source

DbConnection

 DbConnection (params:dict)

Class to create a connection to the database

We can create a DbConnection like this:

conn = DbConnection(params)

Now we want to be able to pull data from the database to use and analyze. We can do this by passing our connection and table name to the pull_data function. Look at the data notebook to see this in action