tmp-connection-psql


Nametmp-connection-psql JSON
Version 1.3.0 PyPI version JSON
download
home_pagehttps://gitlab.obspm.fr/uchosson/tmp_connection_psql
SummaryLittle project to create a temparory connection to a psql database
upload_time2023-01-20 13:06:06
maintainer
docs_urlNone
authorCHOSSON Ulysse
requires_python>=3.10,<4.0.0
licenseEUPL v1.2
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # 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.
```shell
pip install tmp-connection-psql
```

First install all dependencies
```bash
$ 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.

```python
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")`
```python
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
```python
cursor.execute("SELECT * FROM people")
    record = cursor.fetchall()
print(record)
```
it will give you.
```python
[
    ("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:
```python
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
```python
[
    ("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' .
```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

- [Changelog](CHANGELOG.md)
- [EUPL European Union Public License v. 1.2](LICENSE.md)

## Credits

- Author : CHOSSON Ulysse
- Maintainer : CHOSSON Ulysse
- Email : <ulysse.chosson@obspm.fr>
- Contributors :
    - MARTIN Pierre-Yves <pierre-yves.martin@obspm.fr>

            

Raw data

            {
    "_id": null,
    "home_page": "https://gitlab.obspm.fr/uchosson/tmp_connection_psql",
    "name": "tmp-connection-psql",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.10,<4.0.0",
    "maintainer_email": "",
    "keywords": "",
    "author": "CHOSSON Ulysse",
    "author_email": "ulysse.chosson@obspm.fr",
    "download_url": "https://files.pythonhosted.org/packages/dc/f4/d5a8c581efc26431d56a8453267d17eda7c1d1048e54d1465969cd4a63b9/tmp_connection_psql-1.3.0.tar.gz",
    "platform": null,
    "description": "# tmp_connection_psql\n\nIts a little package to create a temporary psql database and qet a connection on it.\n\n## Install\n\nAvailable as a package on pypi.\n```shell\npip install tmp-connection-psql\n```\n\nFirst install all dependencies\n```bash\n$ poetry install\nInstalling dependencies from lock file\n\nNo dependencies to install or update\n\nInstalling the current project: tmp_connection_psql (1.0.1)\n```\n\n## Usage\n\ntmp_connection is a function who yield a connection, to use it you need to make your code in a with\nstatement.\n\n```python\nwith tmp_connection(\"dummypassword\") as conn:\n    cursor = conn.cursor()\n    cursor.execute(\"SELECT * FROM people\")\n    record = cursor.fetchall()\nprint(record)\n```\nGive you an error because the database is empty.\nIt doesn't known the table 'people'.\n\nYou can create your table and fill it after creating the database with `tmp_connection(\"password\")`\n```python\nwith tmp_connection(\"dummypassword\") as conn:\n    cursor = conn.cursor()\n    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)\")\n    cursor.execute(\"\"\"INSERT INTO people VALUES\n(\"Ulysse\", 25, 75019, \"Paris\"), (\"Jacques\", 84, 42820, \"Ambierle\")\"\"\")\n    conn.commit()\n```\nand launch request on it\n```python\ncursor.execute(\"SELECT * FROM people\")\n    record = cursor.fetchall()\nprint(record)\n```\nit will give you.\n```python\n[\n    (\"id\": 1, \"first_name\": \"Ulysse\", \"age\": 25, \"zipcode\": 75019, \"city\": \"Paris\"),\n    (\"id\": 2, \"first_name\": \"Jacques\", \"age\": 84, \"zipcode\": 42820, \"city\": \"Ambierle\"),\n]\n```\n\nOr You can give an sql file to the function `tmp_connection(\"password\", \"./sql_file.sql\")`\nand it will create the table and fill it before giving you access to the connection.\n\nExample:\n```python\nwith tmp_connection(\"dummypassword\", \"./sql_file.sql\") as conn:\n    cursor = conn.cursor()\n    cursor.execute(\"SELECT * FROM people\")\n    record = cursor.fetchall()\nprint(record)\n```\nit will give you\n```python\n[\n    (\"id\": 1, \"first_name\": \"Ulysse\", \"age\": 25, \"zipcode\": 75019, \"city\": \"Paris\"),\n    (\"id\": 2, \"first_name\": \"Jacques\", \"age\": 84, \"zipcode\": 42820, \"city\": \"Ambierle\"),\n]\n```\nwith the file './sql_file.sql' .\n```SQL\n-- Create table\nCREATE 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);\n-- Insert into people\nINSERT INTO people VALUES\n(\"Ulysse\", 25, 75019, \"Paris\"); -- id = 1\n(\"Jacques\", 84, 42820, \"Ambierle\"); -- id = 2\n```\n\n## Changelog, License\n\n- [Changelog](CHANGELOG.md)\n- [EUPL European Union Public License v. 1.2](LICENSE.md)\n\n## Credits\n\n- Author : CHOSSON Ulysse\n- Maintainer : CHOSSON Ulysse\n- Email : <ulysse.chosson@obspm.fr>\n- Contributors :\n    - MARTIN Pierre-Yves <pierre-yves.martin@obspm.fr>\n",
    "bugtrack_url": null,
    "license": "EUPL v1.2",
    "summary": "Little project to create a temparory connection to a psql database",
    "version": "1.3.0",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "5ab267f4f9132180a9dfa5ab912961c3470a0a71b357c7bfb9ca9171d779c54e",
                "md5": "cbef6ed68b2274b14587b3c06b4627c0",
                "sha256": "d2f94a805dda6bcf2b60195127a087e2ff9d53674f516a1ba138c491bd7b945c"
            },
            "downloads": -1,
            "filename": "tmp_connection_psql-1.3.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "cbef6ed68b2274b14587b3c06b4627c0",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.10,<4.0.0",
            "size": 8967,
            "upload_time": "2023-01-20T13:06:04",
            "upload_time_iso_8601": "2023-01-20T13:06:04.352857Z",
            "url": "https://files.pythonhosted.org/packages/5a/b2/67f4f9132180a9dfa5ab912961c3470a0a71b357c7bfb9ca9171d779c54e/tmp_connection_psql-1.3.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "dcf4d5a8c581efc26431d56a8453267d17eda7c1d1048e54d1465969cd4a63b9",
                "md5": "74c4a83a0ee209da529dbd2200e63f37",
                "sha256": "0b3eb925083fcd7ab3b84d2a53f76d1215c275f2e0317193c146e7628988d17b"
            },
            "downloads": -1,
            "filename": "tmp_connection_psql-1.3.0.tar.gz",
            "has_sig": false,
            "md5_digest": "74c4a83a0ee209da529dbd2200e63f37",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.10,<4.0.0",
            "size": 9412,
            "upload_time": "2023-01-20T13:06:06",
            "upload_time_iso_8601": "2023-01-20T13:06:06.077528Z",
            "url": "https://files.pythonhosted.org/packages/dc/f4/d5a8c581efc26431d56a8453267d17eda7c1d1048e54d1465969cd4a63b9/tmp_connection_psql-1.3.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-01-20 13:06:06",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "lcname": "tmp-connection-psql"
}
        
Elapsed time: 0.02852s