Skip to content

Instantly share code, notes, and snippets.

@myedibleenso
Last active August 29, 2025 00:13
Show Gist options
  • Select an option

  • Save myedibleenso/b8c0ebb9817bd818751dca0163480e3e to your computer and use it in GitHub Desktop.

Select an option

Save myedibleenso/b8c0ebb9817bd818751dca0163480e3e to your computer and use it in GitHub Desktop.
Google Sheet -> sqlite DB

The URL structure to download a Google sheet to a csv is https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key=yourDocId&exportFormat=csv&sheet=1

The URL structure to view a Google shet is https://docs.google.com/spreadsheets/d/yourDocId

import pandas as pd
import sqlite3

# pip install "pandas[excel]"

# connect to database
db_name = "lynch"
conn = sqlite3.connect(f"{db_name}.db")


def mk_gsheets_url(doc_id: str, gid: int) -> str: 
  """Create a CSV download link for a single sheet of a Google Sheet"""
  return f"https://spreadsheets.google.com/feeds/download/spreadsheets/Export?key={doc_id}&exportFormat=csv&gid={str(gid)}"


def populate_db(table_name: str, doc_id: str, gid: int, conn) -> pd.core.frame.DataFrame:
  """Populate a SQLite DB from a Google Sheet"""
  df = pd.read_csv(mk_gsheets_url(doc_id=doc_id, gid=gid))
  df.to_sql(name=table_name, con=conn, if_exists="replace")
  return df

# populate DB from David Lynch spreadsheet
tables = [("lynch", 0), ("actors", 485988046)]
for table_name, gid in tables:
  _ = populate_db(table_name=table_name, doc_id="1sVvfAGxBdr2R2aiCkG4_STAvIJLBwH3HTADElsagey4", gid=gid, conn=conn)


# display contents of table
for table, _ in tables:
  print(conn.execute(f"SELECT * FROM '{table}';").fetchall())

To write several DFs to a db, see the following solution: https://stackoverflow.com/q/68705428

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment