Skip to content

Instantly share code, notes, and snippets.

@macloo
Created September 10, 2019 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macloo/1d5e491d058fddb695c0cb1e3a30b0a5 to your computer and use it in GitHub Desktop.
Save macloo/1d5e491d058fddb695c0cb1e3a30b0a5 to your computer and use it in GitHub Desktop.
Basic Python 3 for a SQLite db
import sqlite3
# no need to pip-install; comes w/ Python 3
# from https://www.youtube.com/watch?v=o-vsdfCBpsU
# db connection
# it's fine if the db named here doesn't exist yet - will be created
conn = sqlite3.connect('tutorial.db')
# define cursor
c = conn.cursor()
# create a table
def create_table():
c.execute('CREATE TABLE IF NOT EXISTS stuffToPlot(unix REAL, datestamp TEXT, keyword TEXT, value REAL)')
# enter data - params is a tuple
def data_entry(params):
c.execute( 'INSERT INTO stuffToPlot VALUES (?, ?, ?, ?)', params)
# any change to the db requires a commit
conn.commit()
# run the functions
create_table()
# NOTE parens inside parens!!
data_entry( (908377123, "2019-8-30", "Swift", 1) )
data_entry( (459923098, "2019-9-10", "Go", 1) )
# close the cursor
c.close()
# close the db connection
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment