sqlutilpy


Namesqlutilpy JSON
Version 0.21.0 PyPI version JSON
download
home_pagehttps://github.com/segasai/sqlutilpy
SummaryDatabase query code returning numpy arrays
upload_time2023-11-29 18:46:05
maintainer
docs_urlNone
authorSergey Koposov
requires_python
licenseBSD
keywords numpy postgresql query sql sqlite array
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            [![Build Status](https://github.com/segasai/sqlutilpy/workflows/Testing/badge.svg)](https://github.com/segasai/sqlutilpy/actions)
[![Documentation Status](https://readthedocs.org/projects/sqlutilpy/badge/?version=latest)](http://sqlutilpy.readthedocs.io/en/latest/?badge=latest)
[![Coverage Status](https://coveralls.io/repos/github/segasai/sqlutilpy/badge.svg?branch=master)](https://coveralls.io/github/segasai/sqlutilpy?branch=master)
[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.6867957.svg)](https://doi.org/10.5281/zenodo.6867957)

# sqlutilpy
Python module to query SQL databases and return numpy arrays, upload
tables and run join queries involving local arrays and the tables in the DB.
The module only works with PostgreSQL and SQLite databases.

The full documentation is available [here](http://sqlutilpy.readthedocs.io/en/latest/)

Author: Sergey Koposov (Uni of Cambridge/CMU/Uni of Edinburgh)

## Installation
To install the package you just need to do pip install. 

```
pip install sqlutilpy
```
## Authentification
Throughout this readme, I'll assume that the .pgpass file ( https://www.postgresql.org/docs/11/libpq-pgpass.html ) 
has been created with the login/password details for Postgresql. If that is not the case, all of the 
commands given above will also need user='....' and password='...' options

## Connection information

Most of the sqlutilpy commands require hostname, database name, user etc. 
If you don't want to always type it, you can use standard PostgreSQL environment variables
like PGPORT, PGDATABASE, PGUSER, PGHOST for the port, database name, user name and hostname
of the connection. 


## Querying the database and retrieving the results
```python
import sqlutilpy
ra,dec = squtilpy.get('select ra,dec from mytable', 
                 host='HOST_NAME_OF_MY_PG_SERVER', 
                 db='THE_NAME_OF_MY_DB')
```

By default sqlutilpy.get executes the result and returns the tuple of 
results. But you can return the results as dictionary using asDict option.

## Uploading your arrays as column in a table
```python
x = np.arange(10)                                                   
y = x**.5                                                           
sqlutilpy.upload('mytable',(x,y),('xcol','ycol'))    
``` 
This will create a table called mytable with columns xcol and ycol 

## Join query involving your local data and the database table

Imagine you have arrays myid and y and you want to to extract all the 
information from somebigtable for objects with id=myid. In principle
you could upload the arrays in the DB and run a query, but local_join function does that for you.

```python
myid = np.arange(10)
y = x**.5
R=sqlutilpy.local_join('''select * from mytmptable as m, 
           somebigtable as s where s.id=m.myid order by m.myid''',                                                                       'mytmptable',(x,y),('myid','ycol'))
```
It executes a query as if you arrays where in a mytmptable. ( behind the scenes
it uploads the data to the db and runs a query)


## Keeping the connection open. 

Often it's benefitial to preserve an open connection. You can do that if you first 
obtain the connection using sqlutilpy.getConnection() and then provide it directly
to sqlutil.get() and friends using conn=conn argument


# How to cite it
If you use this, please cite it through zenodo https://doi.org/10.5281/zenodo.6867957

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/segasai/sqlutilpy",
    "name": "sqlutilpy",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "numpy postgresql query sql sqlite array",
    "author": "Sergey Koposov",
    "author_email": "skoposov@ed.ac.uk",
    "download_url": "https://files.pythonhosted.org/packages/29/22/458e40ab0940f0464fc18515d4f090b173e75b534bb78fa0cf4986d03a7e/sqlutilpy-0.21.0.tar.gz",
    "platform": null,
    "description": "[![Build Status](https://github.com/segasai/sqlutilpy/workflows/Testing/badge.svg)](https://github.com/segasai/sqlutilpy/actions)\n[![Documentation Status](https://readthedocs.org/projects/sqlutilpy/badge/?version=latest)](http://sqlutilpy.readthedocs.io/en/latest/?badge=latest)\n[![Coverage Status](https://coveralls.io/repos/github/segasai/sqlutilpy/badge.svg?branch=master)](https://coveralls.io/github/segasai/sqlutilpy?branch=master)\n[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.6867957.svg)](https://doi.org/10.5281/zenodo.6867957)\n\n# sqlutilpy\nPython module to query SQL databases and return numpy arrays, upload\ntables and run join queries involving local arrays and the tables in the DB.\nThe module only works with PostgreSQL and SQLite databases.\n\nThe full documentation is available [here](http://sqlutilpy.readthedocs.io/en/latest/)\n\nAuthor: Sergey Koposov (Uni of Cambridge/CMU/Uni of Edinburgh)\n\n## Installation\nTo install the package you just need to do pip install. \n\n```\npip install sqlutilpy\n```\n## Authentification\nThroughout this readme, I'll assume that the .pgpass file ( https://www.postgresql.org/docs/11/libpq-pgpass.html ) \nhas been created with the login/password details for Postgresql. If that is not the case, all of the \ncommands given above will also need user='....' and password='...' options\n\n## Connection information\n\nMost of the sqlutilpy commands require hostname, database name, user etc. \nIf you don't want to always type it, you can use standard PostgreSQL environment variables\nlike PGPORT, PGDATABASE, PGUSER, PGHOST for the port, database name, user name and hostname\nof the connection. \n\n\n## Querying the database and retrieving the results\n```python\nimport sqlutilpy\nra,dec = squtilpy.get('select ra,dec from mytable', \n                 host='HOST_NAME_OF_MY_PG_SERVER', \n                 db='THE_NAME_OF_MY_DB')\n```\n\nBy default sqlutilpy.get executes the result and returns the tuple of \nresults. But you can return the results as dictionary using asDict option.\n\n## Uploading your arrays as column in a table\n```python\nx = np.arange(10)                                                   \ny = x**.5                                                           \nsqlutilpy.upload('mytable',(x,y),('xcol','ycol'))    \n``` \nThis will create a table called mytable with columns xcol and ycol \n\n## Join query involving your local data and the database table\n\nImagine you have arrays myid and y and you want to to extract all the \ninformation from somebigtable for objects with id=myid. In principle\nyou could upload the arrays in the DB and run a query, but local_join function does that for you.\n\n```python\nmyid = np.arange(10)\ny = x**.5\nR=sqlutilpy.local_join('''select * from mytmptable as m, \n           somebigtable as s where s.id=m.myid order by m.myid''',                                                                       'mytmptable',(x,y),('myid','ycol'))\n```\nIt executes a query as if you arrays where in a mytmptable. ( behind the scenes\nit uploads the data to the db and runs a query)\n\n\n## Keeping the connection open. \n\nOften it's benefitial to preserve an open connection. You can do that if you first \nobtain the connection using sqlutilpy.getConnection() and then provide it directly\nto sqlutil.get() and friends using conn=conn argument\n\n\n# How to cite it\nIf you use this, please cite it through zenodo https://doi.org/10.5281/zenodo.6867957\n",
    "bugtrack_url": null,
    "license": "BSD",
    "summary": "Database query code returning numpy arrays",
    "version": "0.21.0",
    "project_urls": {
        "Homepage": "https://github.com/segasai/sqlutilpy"
    },
    "split_keywords": [
        "numpy",
        "postgresql",
        "query",
        "sql",
        "sqlite",
        "array"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "dea8dfcdad4a6d59d810bb7fa10c4fe770ed098bf535040411b430fe9d54b47a",
                "md5": "6cd604afc51fea585ca1804b0784644b",
                "sha256": "b811b462eeac1539b384cfeaee02e3099dc17ec0d8afd34cf3aad7ad4c80562f"
            },
            "downloads": -1,
            "filename": "sqlutilpy-0.21.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "6cd604afc51fea585ca1804b0784644b",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 11284,
            "upload_time": "2023-11-29T18:46:03",
            "upload_time_iso_8601": "2023-11-29T18:46:03.464325Z",
            "url": "https://files.pythonhosted.org/packages/de/a8/dfcdad4a6d59d810bb7fa10c4fe770ed098bf535040411b430fe9d54b47a/sqlutilpy-0.21.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "2922458e40ab0940f0464fc18515d4f090b173e75b534bb78fa0cf4986d03a7e",
                "md5": "0e0021d05ab1e6a3396a63303ea63ff1",
                "sha256": "b2781613ad45c2d05c426c4258ed1ac9c88ec288cf5fe3884cf191ed29d5559f"
            },
            "downloads": -1,
            "filename": "sqlutilpy-0.21.0.tar.gz",
            "has_sig": false,
            "md5_digest": "0e0021d05ab1e6a3396a63303ea63ff1",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 16199,
            "upload_time": "2023-11-29T18:46:05",
            "upload_time_iso_8601": "2023-11-29T18:46:05.841350Z",
            "url": "https://files.pythonhosted.org/packages/29/22/458e40ab0940f0464fc18515d4f090b173e75b534bb78fa0cf4986d03a7e/sqlutilpy-0.21.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-11-29 18:46:05",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "segasai",
    "github_project": "sqlutilpy",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "requirements": [],
    "lcname": "sqlutilpy"
}
        
Elapsed time: 0.12718s