Skip to content

Instantly share code, notes, and snippets.

@simonw
Last active January 18, 2021 04:45
Show Gist options
  • Save simonw/0df922918cc653e73baa8d003df4d872 to your computer and use it in GitHub Desktop.
Save simonw/0df922918cc653e73baa8d003df4d872 to your computer and use it in GitHub Desktop.
Updating SQLite using a custom function and a click-powered progress bar
import click
import sqlite3
import dateparser
count = 0
c = sqlite3.connect("tweets.db")
with click.progressbar(
length=c.execute("select count(*) from tweets").fetchone()[0],
label='Updating dates'
) as bar:
def dateparser_parse(s):
bar.update(1)
return dateparser.parse(s).isoformat()
c.create_function("dateparser_parse", 1, dateparser_parse)
with c:
c.execute("""
update tweets set
publish_date=dateparser_parse(publish_date);
""")
@simonw
Copy link
Author

simonw commented Jan 18, 2021

I turned this into a tool: https://github.com/simonw/sqlite-transform/

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