michalc / sqlite-s3-query

Python functions to query SQLite files stored on S3
MIT License
251 stars 15 forks source link

Queries with params don't seem to work #39

Closed Mjboothaus closed 2 years ago

Mjboothaus commented 2 years ago

Example query syntax:

'SELECT "Beach name" FROM beaches WHERE Region = ?, params=("SydneyOceanBeaches", )'

[ISSUE reporting in progress - add environment details & error message]

michalc commented 2 years ago

Hello 👋

It's hard to be sure from your example, but it looks a bit like params=("SydneyOceanBeaches", ) is passed inside the SQL string itself, rather than as a separate argument to the query function.

So it looks like you're trying:

from sqlite_s3_query import sqlite_s3_query

with sqlite_s3_query(url='https://my-bucket.s3.eu-west-2.amazonaws.com/my-db.sqlite') as query:
    with query('SELECT "Beach name" FROM beaches WHERE Region = ?, params=("SydneyOceanBeaches", )') as (columns, rows):
        for row in rows:
            print(row)

when it should be something like:

from sqlite_s3_query import sqlite_s3_query

with sqlite_s3_query(url='https://my-bucket.s3.eu-west-2.amazonaws.com/my-db.sqlite') as query:
    with query('SELECT "Beach name" FROM beaches WHERE Region = ?', params=("SydneyOceanBeaches", )) as (columns, rows):
        for row in rows:
            print(row)
Mjboothaus commented 2 years ago

G'day Michal

Thanks for you prompt reply -- yes I was mistakenly passing the params within the SQL query string. It is working well now.

Any thoughts on returning the (columns, rows) as a pandas dataframe? :)

michalc commented 2 years ago

Any thoughts on returning the (columns, rows) as a pandas dataframe? :)

Like this?

import pandas as pd

# ...

with query('SELECT "Beach name" FROM beaches WHERE Region = ?', params=("SydneyOceanBeaches", )) as (columns, rows):
    df = pd.DataFrame(rows, columns=columns)
michalc commented 2 years ago

Oh just realised - were you asking about changing the API of sqlite-s3-query to return a pandas DataFrame? If so: I'm fairly anti:

Anyway, suspect in any case this is beyond the scope of this particular issue. Closing, but feel free to open another.