Execute a psql script within a Python scriptSee original GitHub issue
I would like to Insert JAX into the ‘team’ table inside the nfldb-update script
psql -U nfldb -f C:\Python27\Insert_JAX.sql
where Insert_JAX.sql is
INSERT into team values('JAX','Jacksonville', 'Jaguars')
Is this possible? And could someone help me with the correct syntax to do this?
- Created 7 years ago
Top GitHub Comments
Yep that should work. Remember to close the cursor and connection when you’re done with
I would also recommend moving the password into a
.pgpass file(google it for configuration) for easier access and more security so you don’t litter your scripts with your db pass. Your connection string will then become
conn = psycopg2.connect("host=localhost dbname=nfldb user=nfldb") You can query with
week = 5 cur.execute("""SELECT * FROM game WHERE week = %s LIMIT 5;""",(week,)) print cur.fetchall()
update.py seems to be closing the cursor and connection on its own. I get an error that they are already closed so I left it out.
My authentication method is set to trust in the
pg_hba.conf file so no password is needed.
Currently my program downloads the stats of every game in progress every 10 seconds but I think a more efficient way would be to monitor ‘some_file’ for a change in the modified time, indicating a change in a game in progress (score, down, end of quarter, etc.)
Just not sure which file would be the best one to monitor.