Abstraction layer for PostgreSQL database manipulation based on the psycopg2 package
Abstraction layer for PostgreSQL database manipulation based on the
Primary focuses are lightweight API and high flexibility when it comes to handling user input. This should make the package very easy to use while maintaining a usable performance level.
- Easy Postgres
WARNING: Work in Progress
This package is currently under heavy development and should NOT be used in production. The current API is very likely to change in the near future.
How To Use
To do anything, you must first create an instance of the
Connection class. Give the instructor a DSN. The DSN can take many forms, so please check the
psycopg2.connect function documentation for more details.
Once you have a
Connection, you are ready to run any common SQL query (
DELETE should definitely work; the rest should probably work, but it is not guaranteed).
Executes the specified SQL query and returns nothing (
None). This is the right choice for all non-returning queries (
DROP, etc.). An
INSERT query with a
RETURNING clause should use the
Connection.one method instead.
Executes the specified SQL query and returns a single row. If the returned row only has a single column, the single value will be returned. In the case of multiple returned columns, a tuple containing all the column values will be returned.
If the number of returned rows is not exactly equal to one,
None will be returned instead.
This method is meant to be used for SQL queries that are guaranteed to always return exactly one row (e.g.,
SELECT COUNT(*) FROM table;,
INSERT INTO table (id) VALUES (1) RETURNING id;).
|SQL Query||Returned Result|
Executes the specified SQL query and returns all the rows as a list. Single-column results will be converted to a list of the column's values. The behavior is similar to the
This method is mostly meant to be used for
SELECT queries that may return more (or less) than one row.
Executes the specified SQL query and returns all the rows as a generator. This method is an alternative to
Connection.all to be used when iterating over a large number of rows. The rows are fetched one at a time, so casting the generator to a list will give the same exact result as calling
Connection.all would have, but it will likely be much slower.
These methods execute the SQL queries just like their non-dict counterparts, but instead of returning a value or a tuple, they return a
It is advisable to give all the returned columns a unique name without any special characters (
_ is the only allowed exception). If you include any special character in the column name or if the column name is a Python keyword (
for, etc.) or the name of a built-in method name (
__str__, etc.), the column value will become inaccessible as an attribute, or worse yet, some functionality of the
Dictionary class may be broken. Please keep this in mind when using the
An extension of the Python's built-in
dict class. It makes the items accessible via attributes (the item's key is used as the attribute name). There are several limitations, but the code should be slightly more readable. The support for the default dictionary item access (via an index) remains.
Dictionary can be initialized from a regular dictionary, using keyword arguments or a combination of both.
For the purpose of this example, imagine you ran an SQL query that returned a single row as a
Dictionary. The query result is stored in a variable called
result. Now you want to print the value of the
price column. Here is a comparison of
You can use whichever way of accessing the items you prefer. If you want, you can even cast the
Dictionary back to a
dict for better compatibility. Being able to access items as attributes simply seems more convenient and easier to read.
This class can be used to execute several SQL queries in a series without intermediate commits. If an exception occurs during any of the queries, all the changes made during the transaction will be rolled back. Otherwise, the changes will be committed all at once when you exit the transaction block. This prevents committing only partial changes when an error occurs, which could lead to an unexpected state of the database.
Keep in mind that this
Transaction class is by no means related to the SQL
Instances of this class are only meant to be used in the head of
with blocks (see code below for intended usage). It is preferable to create a
Transaction by calling the
Connection.transaction method instead of calling the constructor manually (even though there is no difference as of writing this). Please do not create multiple
Transactions at once using the same
conn = Connection("postgresql:///db") with conn.transaction(): row_id = conn.one("INSERT INTO table (id, value) VALUES (10, 'Hello World!') RETURNING id;") prev_id = conn.one("SELECT MAX(id) FROM table WHERE id < %s;", row_id) conn.run("UPDATE table SET value = 'New Value' WHERE id = %s;", prev_id)
This is perhaps kind of a silly code, but it demonstrates the functionality. If one of the queries fails (throws an exception), no changes will be committed. If they all succeed, the changes will be made after the last query is executed and the
with block is exited.
This package is available under the MIT License.
Release history Release notifications | RSS feed
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size easy_postgres-0.0.14-py3-none-any.whl (8.9 kB)||File type Wheel||Python version py3||Upload date||Hashes View|
|Filename, size easy_postgres-0.0.14.tar.gz (7.0 kB)||File type Source||Python version None||Upload date||Hashes View|
Hashes for easy_postgres-0.0.14-py3-none-any.whl