Skip to main content

Build Status Documentation Status Coverage Status DOI

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. This module is optimized to deal efficiently with query results with millions of rows. The module works with PostgreSQL, SQLite and DuckDB databases.

The full documentation is available here

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

Authentication

Throughout this readme, I will assume that if you are using PostgreSQL, then the .pgpass file ( https://www.postgresql.org/docs/11/libpq-pgpass.html ) has been created with your login/password details for Postgresql. If that is not the case, many of the commands given below will also need user='....' and password='...' options.

Connection information

Most of the sqlutilpy commands require hostname, database name.
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

This command will run the query and put the columns into variables ra,dec:

import sqlutilpy
ra,dec = sqlutilpy.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 query and returns the tuple with arrays. One array for each column in the query result. You can return the results as dictionary using asDict option.

SQLite and DuckDB

The same get() and execute() functions work with SQLite and DuckDB files, by selecting the driver and giving the file name as the database:

a, b = sqlutilpy.get('select a, b from mytable', db='mydata.db', driver='sqlite3')
a, b = sqlutilpy.get('select a, b from mytable', db='mydata.duckdb', driver='duckdb')

Uploading tables and local_join are only available with PostgreSQL.

Uploading your arrays as columns in a table (PostgreSQL)

You can use sqlutilpy.upload to upload your arrays as columns in a table.

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 (PostgreSQL)

Sometimes it is beneficial to run a join query involving your local data and the data in the database.

Imagine you have arrays myid and y and you want 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.

myid = np.arange(10)
y = np.random.uniform(size=10)

R=sqlutilpy.local_join('''select * from mytmptable as m, 
           somebigtable as s where s.id=m.myid order by m.myid''',                                              
           'mytmptable',(myid, y),('myid','ycol'))

It executes a query as if your arrays were in mytmptable. What happens behind the scenes is that it uploads the data to the database and runs a query against it.

Keeping the connection open.

Often it is beneficial to preserve an open connection to the database. You can do that if you first obtain the connection using sqlutilpy.getConnection() and then provide it directly to sqlutilpy.get() and similar commands using conn= keyword:

conn = sqlutilpy.getConnection(db='mydb', user='meuser', password='something', host='hostname')
R= sqlutilpy.get('select 1', conn=conn)
R1= sqlutilpy.get('select 1', conn=conn)

How to cite the software

If you use this package, please cite it through Zenodo https://doi.org/10.5281/zenodo.5160118

Metadata

Release files for sqlutilpy 0.30.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sqlutilpy 0.30.0
File Size Uploaded
sqlutilpy-0.30.0.tar.gz 28.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sqlutilpy 0.30.0
File Interpreter ABI Platform
sqlutilpy-0.30.0-py3-none-any.whl Python 3 none any Details

Total release size: 44.3 kB

Release files / sqlutilpy-0.30.0.tar.gz

Download URL sqlutilpy-0.30.0.tar.gz
Size 28.8 kB
Tags Source
SHA-256 checksum
How to use checksums
0c8f54bca924507f7154f6ae5b1dcd859d1c3f0ad00ee07359b6d2708af5c804
BLAKE2b-256 checksum
How to use checksums
520dd6b3561d4be81c29d7f46d7ba97d71ef0d6b35c67c508b80bd183f4a83c9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / sqlutilpy-0.30.0-py3-none-any.whl

Download URL sqlutilpy-0.30.0-py3-none-any.whl
Size 15.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8eb4e2d5d31bed0fe055ee96a41e527e386d3bfe25eaa1fb8485404f629b6863
BLAKE2b-256 checksum
How to use checksums
5738e5bc9ee79b95ef3a5d5e4ab506d352b0c04ae70681ce0bf7b852c70e2e1a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release history Release notifications | RSS feed

This release

0.30.0 This release

2 release files

0.29.0

2 release files

0.28.0

2 release files

0.27.0

2 release files

0.25.0

2 release files

0.24.0

2 release files

0.23.0

2 release files

0.22.0

2 release files

0.21.0

2 release files

0.20.0

2 release files

0.19.0

2 release files

0.16.0

2 release files

0.15.0

2 release files

0.14.0

2 release files

0.13.0

1 release file

0.11.0

1 release file

0.10.0

1 release file

0.0.9

1 release file

0.0.8

1 release file

0.0.7

1 release file

0.0.6

1 release file

0.0.5

1 release file

0.0.4

1 release file

0.0.3

1 release file

0.0.2

1 release file

0.0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page