Skip to main content

Little project to create a temparory connection to a psql database

Project description

tmp_connection_psql

Its a little package to create a temporary psql database and qet a connection on it.

Install

Available as a package on pypi.

pip install tmp-connection-psql

First install all dependencies

$ poetry install
Installing dependencies from lock file

No dependencies to install or update

Installing the current project: tmp_connection_psql (1.0.1)

Usage

tmp_connection is a function who yield a connection, to use it you need to make your code in a with statement.

with tmp_connection("dummypassword") as conn:
    cursor = conn.cursor()
    cursor.execute("SELECT * FROM people")
    record = cursor.fetchall()
print(record)

Give you an error because the database is empty. It doesn't known the table 'people'.

You can create your table and fill it after creating the database with tmp_connection("password")

with tmp_connection("dummypassword") as conn:
    cursor = conn.cursor()
    cursor.execute("CREATE TABLE people (id serial NOT NULL PRIMARY KEY, first_name TEXT NOT NULL, age int NOT NULL, zipcode int NOT NULL, city TEXT NOT NULL)")
    cursor.execute("""INSERT INTO people VALUES
("Ulysse", 25, 75019, "Paris"), ("Jacques", 84, 42820, "Ambierle")""")
    conn.commit()

and launch request on it

cursor.execute("SELECT * FROM people")
    record = cursor.fetchall()
print(record)

it will give you.

[
    ("id": 1, "first_name": "Ulysse", "age": 25, "zipcode": 75019, "city": "Paris"),
    ("id": 2, "first_name": "Jacques", "age": 84, "zipcode": 42820, "city": "Ambierle"),
]

Or You can give an sql file to the function tmp_connection("password", "./sql_file.sql") and it will create the table and fill it before giving you access to the connection.

Example:

with tmp_connection("dummypassword", "./sql_file.sql") as conn:
    cursor = conn.cursor()
    cursor.execute("SELECT * FROM people")
    record = cursor.fetchall()
print(record)

it will give you

[
    ("id": 1, "first_name": "Ulysse", "age": 25, "zipcode": 75019, "city": "Paris"),
    ("id": 2, "first_name": "Jacques", "age": 84, "zipcode": 42820, "city": "Ambierle"),
]

with the file './sql_file.sql' .

-- Create table
CREATE TABLE people (id serial NOT NULL PRIMARY KEY, first_name TEXT NOT NULL, age int NOT NULL, zipcode int NOT NULL, city TEXT NOT NULL);
-- Insert into people
INSERT INTO people VALUES
("Ulysse", 25, 75019, "Paris"); -- id = 1
("Jacques", 84, 42820, "Ambierle"); -- id = 2

Changelog, License

Credits

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

tmp_connection_psql-1.0.1.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

tmp_connection_psql-1.0.1-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file tmp_connection_psql-1.0.1.tar.gz.

File metadata

  • Download URL: tmp_connection_psql-1.0.1.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.13 CPython/3.10.4 Linux/5.15.0-27-generic

File hashes

Hashes for tmp_connection_psql-1.0.1.tar.gz
Algorithm Hash digest
SHA256 b9590314d20e82b7f90f3e7988faae93ed9e0ed6576dd0f842633b79bd3f8117
MD5 e37e2f4ac9f594af001132a9ebf083b2
BLAKE2b-256 89c1f5a6965a323083871996f1f44407a8c30c15eb809a41024f93e1b286549f

See more details on using hashes here.

File details

Details for the file tmp_connection_psql-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for tmp_connection_psql-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 94bce1b9a615750656a76356d6943c20769d7db29ac8cae527a0bfdeabeba187
MD5 fc83862b052593c3d1f3ab3397273928
BLAKE2b-256 bc64d9be63f26aae6fb64af10c4b52fccdab451058a9ad54f2ca70a8c5a240c8

See more details on using hashes here.

Supported by

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