Skip to main content

A package to perform Postgres CRUD operations

Project description

pgcrud

Leverage python lists and dicts to more dynamically perform CRUD operations on postgres databases.

installation

pip install pgcrud

connect

from pgcrud.connector import Connector

conn = Connector(host, dbname, user, password)

create()

Pass in the name of the table and a dictionary containing the database field name as the key and the value to set as the value.

from pgcrud.connector import Connector

conn = Connector(host, dbname, user, password)
data = {'firstname': 'Fred', 'lastname': 'Jones'}
conn.create('contact', data)

criteria

The update, delete and select functions are frequently used with criteria.

To pass the criteria replace all of the criteria values with %s and pass a list of the values in the order they are used in the criteria. This enables the psycopg2 connector to makes sure the values are database safe and formatted appropriately based on their type.

IN criteria should be passed in with one %s where they should go and a list entry in the values.

from pgcrud.connector import Connector

conn = Connector(host, dbname, user, password)
fields = ['firstname', 'lastname']
criteria = 'WHERE country = %s AND state in %s'
values = ['United States', ['MA', 'MI', 'NY']]
results = conn.select('contact', fields, criteria, values)

This would form the SQL:

SELECT firstname,
       lastname
    FROM contact
    WHERE country = 'United States'
    AND state in ('MA', 'MI', 'NY')

update()

Pass in the name of the table and a dictionary containing the database field name as the key and the value to set as the value to update.

from pgcrud.connector import Connector

conn = Connector(host, dbname, user, password)
data = {'firstname': 'Fred', 'lastname': 'Jones'}
criteria = 'WHERE id = %s'
values = [12436]
conn.update('contact', data, criteria, values)

delete()

Pass in the name of the table, the criteria and values to identify which records to delete.

from pgcrud.connector import Connector

conn = Connector(host, dbname, user, password)
criteria = 'WHERE id = %s'
values = [12436]
conn.delete('contact', criteria, values)

select()

Pass in the name of the table, a list of the field names you want and the criteria and values to identify which records to select.

A list of dictionaries will be returned. Each entry will have the database field name as the key and the retrieved value as the value.

from pgcrud.connector import Connector

conn = Connector(host, dbname, user, password)
fields = ['firstname', 'lastname]
criteria = 'WHERE id = %s'
values = [12436]
results = conn.select('contact', fields, criteria, values)

commit()

The create, delete and select funcitons have an optional commit parameter. Sometimes when creating or deleting records in a database you want to do an all or none transaction. For example you are creating an order and all of its items. If one of the items fails to create you do not want the order to be created. In this case you would pass commit = False. This means that the insertions will not be committed until you say so, when they are all complete.

This parameter is also included in the select function because you may need information from one of the created records to create further records. For example you insert the order and need its id in order to associate its items with it. In this case you would also want to pass commit = False to the select function, so that when you are retrieving the order id the order insert is not committed.

from pgcrud.connector import Connector

conn = Connector(host, dbname, user, password)
data = {'total': 1900, 'contact_id': 234422, 'external_id' = 34567}
conn.create('order', data, False)```
order = conn.select('order', ['id'], 'WHERE external_id = %s', [data['external_id]], False)
order_id = order[0]['id']
items = [{'sku': 'CUCUMBER',
          'qty': 1,
          'price': .5},
          {'sku': 'GRAPEFRUIT',
          'qty': 2,
          'price': 1}]
for item in items:
    item['order_id'] = order_id
    conn.create('item', item, False)
conn.commit()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pgcrud-0.0.5.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pgcrud-0.0.5-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file pgcrud-0.0.5.tar.gz.

File metadata

  • Download URL: pgcrud-0.0.5.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.6.8

File hashes

Hashes for pgcrud-0.0.5.tar.gz
Algorithm Hash digest
SHA256 8db12ef7462b841c87dde6641d9209dd2098a85b02c6b8a46ee71c8b267711a6
MD5 e090e975d30aa509893eae85616c3a49
BLAKE2b-256 03711c0f83bb1458d700baa42447956b51e0d6530fc4ab57decae375092fb8b5

See more details on using hashes here.

File details

Details for the file pgcrud-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: pgcrud-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.6.8

File hashes

Hashes for pgcrud-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 23bdd18e1a23c1a81b14b434cb3f71cef497cdf8cf0c560ebe693e178543ef7b
MD5 214aa7db2e19723385d13ed75041507f
BLAKE2b-256 8fa29865cc4db72ce718fcfcab7802e9d5d5f37e0150b22c6c7cca208155ee72

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page