Skip to content

Instantly share code, notes, and snippets.

@rdapaz
Created May 21, 2018 16:43
Show Gist options
  • Save rdapaz/0b92c0c8856315ff3c5b86e2d5c7258d to your computer and use it in GitHub Desktop.
Save rdapaz/0b92c0c8856315ff3c5b86e2d5c7258d to your computer and use it in GitHub Desktop.
Using PostgreSQL from Python
import psycopg2
conn = psycopg2.connect("dbname='timesheets' user=postgres")
if True:
cur = conn.cursor()
sql = """
CREATE TABLE IF NOT EXISTS \"public\".\"ts\" (
id serial primary key,
dt date,
project text,
consultant text,
days decimal
)
"""
cur.execute(sql)
sql = """ INSERT INTO \"public\".\"ts\" (
dt, project, consultant, days
) VALUES
(%s, %s, %s, %s)
"""
cur.executemany(sql, data)
conn.commit()
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment