Skip to main content

Use Google Sheets like a PostgreSQL database

Project description

gsheet-sql-python

Query Google Sheets with SQL. Treat each spreadsheet as a database, each tab as a table — run SELECT, INSERT, UPDATE, DELETE, filter with a Python API, and explore data in an interactive REPL.

pip install gsheet-sql-python

Getting started

from gsheets_sql import connect

db = connect(
    spreadsheet_id="1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgVE2upms",
    credentials="credentials.json",
)

credentials.json can be a service account key or OAuth 2.0 client secrets — a browser window opens on first OAuth use and the token is cached locally.


SQL queries

# Returns a list of dicts by default
rows = db.query("SELECT nome, idade FROM clientes WHERE idade > 30 ORDER BY nome LIMIT 10")

# Or a pandas DataFrame
df = db.query("SELECT * FROM clientes", as_dataframe=True)

Supported: WHERE, ORDER BY, GROUP BY, LIMIT, OFFSET, COUNT, SUM, AVG, MIN, MAX, JOIN between tabs.

DML

db.execute("INSERT INTO clientes (nome, idade) VALUES ('Ana', 28)")
db.execute("UPDATE clientes SET idade = 29 WHERE nome = 'Ana'")
db.execute("DELETE FROM clientes WHERE nome = 'Ana'")

Python API

table = db["clientes"]

table.all()                                  # → DataFrame
table.filter(idade__gt=30, ativo=True)       # ORM-style filters
table.get(nome="Ana")                        # first match

table.insert({"nome": "Ana", "idade": 28})
table.insert_many([{"nome": "Bob"}, {"nome": "Clara"}])
table.update({"idade": 29}, where={"nome": "Ana"})
table.delete(where={"nome": "Ana"})

table.schema()     # inferred column types
table.count()

Supported filter suffixes: __gt, __gte, __lt, __lte, __ne, __like, __ilike, __in, __isnull.

Pandas integration

db.from_dataframe("clientes", df, if_exists="replace")  # replace | append | fail

Schema management

db.tables()                                         # list tabs
db.create_table("pedidos", ["id", "produto", "valor"])
db.drop_table("pedidos")
table.rename_column("preco", "valor")

Interactive REPL

gsheets-sql --id <spreadsheet_id> --credentials credentials.json
MinhaBase> \dt
 Tables
--------
 clientes
 pedidos

MinhaBase> \d clientes
 Column | Type
--------+------
 id     | int
 nome   | str
 idade  | int
 ativo  | bool

MinhaBase> SELECT * FROM clientes WHERE idade > 30;
 nome | idade | ativo
------+-------+-------
 Bob  |    35 | false
(1 row)

MinhaBase> \q

Meta-commands: \dt list tables · \d <table> describe schema · \q quit · \? help.


Configuration

db = connect(
    spreadsheet_id="...",
    credentials="credentials.json",
    cache_ttl=60,    # seconds; 0 disables cache
    header_row=1,    # row that contains column names
)

License

MIT

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

gsheet_sql_python-0.1.1.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

gsheet_sql_python-0.1.1-py3-none-any.whl (13.2 kB view details)

Uploaded Python 3

File details

Details for the file gsheet_sql_python-0.1.1.tar.gz.

File metadata

  • Download URL: gsheet_sql_python-0.1.1.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for gsheet_sql_python-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d7f46073a314591fc5b1ab2052bf78aeb0be1889f62c685df48a83d45000fb40
MD5 598da103e28432c505e5c0e272c0fcf8
BLAKE2b-256 c2216b8a16cbf8956f3be57b6838d02a1f7d99f34e7a66814263597d78b92d21

See more details on using hashes here.

File details

Details for the file gsheet_sql_python-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for gsheet_sql_python-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c6eb8d3eedbb193a3491628a27cc4a7074a04ca4932184b8505c3ad7c2f33813
MD5 62a7759f26527b71de78d5736334dc4d
BLAKE2b-256 e910beafa914751af43ad69c74b9404863783f74d4138ef9ecb00c17529c8929

See more details on using hashes here.

Supported by

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