Library to load and extract data from a PosgreSQL Database with Python with a simple SQL style language
Project description
PostgreSQLInterface
Introduction
This package allows to load and extract data from a PosgreSQL Database using simple SQL style methods. At the moment, only has been implemented for Heroku-Amazon Databases, though it should be easy to extent this package to interact with other vendors as is it created following the Factory Pattern.
Structure
The library has a parent abstract class that contains the main methods, then a children class implements the
particularities of a vendor, finally, a function handles the creation of the objects.
Below, you can find a little introduction to all relevant methods. For more information, read the doc of the method
or take a look to the tests in the repo.
Instantiate the class
To instantiate the class to connect to a Heroku Database:
heroku_db = postgres_factory(vendor='heroku', database_url=database_url)
database_url can be found on the section Config Vars inside the tab Settings of your Heroku app or on the section Database Credentials of the tab Settings of your Heroku Datastore.
Read data from Database
To retrieve a query from the database:
my_table = heroku_db.query("SELECT * FROM test.data")
It is also possible to do more complicated queries:
my_table = heroku_db.query("SELECT * FROM test.data d JOIN test.references r ON d.referencesid = r.id")
Insert data
To insert data into a table:
heroku_db.insert_table('test.simple', to_insert.copy())
Update a table
To update data into a table:
heroku_db.update_table('test.simple', to_update, ['id', 'date'])
Delete from table
To delete from a table:
heroku_db.delete_from_table('test.simple', to_delete)
Execute a general statement
To execute a general SQL statement you can use the method execute. This method returns no data. It is important to notice that it is not the SQL execute command. As an example, you can use it to create a schema:
heroku_db.execute("CREATE SCHEMA test")
You can also use it to execute a stored procedure
heroku_db.execute("EXECUTE test.sp_test1 @input = '%s" @ input)
Tests
To be able to execute the tests, it is necessary to provide a '.env' file with the url to connect to a Heroku database.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for postgresql-interface-1.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2d47dcde513a2ebe19be924af01c52790f459e9f243a18a6fd562217a768ce99 |
|
MD5 | 4e0eb1cc0e5a1286b9289aa346ed99bb |
|
BLAKE2b-256 | 8adde0151f39a53bd9c934eedeb7240b42bf2f2b08f250a9ffae530debd728c8 |
Hashes for postgresql_interface-1.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7dfd88f862c16d7084b364b785e2d93773b4351b0b1ec89b1d13fed3642dc3ec |
|
MD5 | 289917c49c19624b9c2308b1924a637d |
|
BLAKE2b-256 | 62868fd9d1e66ce433c80610985ce7f702c4c25f82239d67cb85ce5749500a6d |