SQL DatabaseAccess Python

From Seamonster
Jump to: navigation, search

refer to SQL_Secrets

fire up python and

heavner@NSRL01:~$ python
Python 2.4.4c1 (#2, Oct 11 2006, 21:51:02) 
>>> import pg
>>> nsrl_met_db = pg.connect('NSRL','127.0.0.1',5432,None,None,'seamonster',password)
>>> data_tail = nsrl_met_db.query("""SELECT * FROM nsrl_met ORDER BY time_epoch DESC LIMIT 5""")
>>> print data_tail

Not all that elegant, but it works. Now to wrangle it into a dataframe so R is happy...

a second example, from Marijke's example:

heavner@NSRL01:~$ python
Python 2.4.4c1 (#2, Oct 11 2006, 21:51:02) 
[GCC 4.1.2 20060928 (prerelease) (Ubuntu 4.1.1-13ubuntu5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pg
>>> nsrl_met_db = pg.connect('seamonster','127.0.0.1',5433,None,None,'seamonster',password)
>>> data_tail = nsrl_met_db.query("""SELECT * FROM data_conversion(1,'2007-08-26', '2007-08-28')""")
>>> print data_tail

Heavner 16:03, 28 September 2009 (PDT)

>>> nsrl_met_db = pg.connect('seamonster','127.0.0.1',5432,None,None,'postgres',password)
>>> data_tail = nsrl_met_db.query("""SELECT * FROM data_conversion(1,'2009-09-01','2009-09-28')""")
>>> print data_tail
timestamp             |data 
----------------------+-----
2009-09-01 00:00:00-08|5.174
2009-09-01 00:15:00-08|5.053
2009-09-01 00:30:00-08|4.981
2009-09-01 00:45:00-08|4.904
2009-09-01 01:00:00-08|4.887
2009-09-01 01:15:00-08|4.76 
2009-09-01 01:30:00-08|4.735



SQL_DatabaseAccess_Python_InstallationNotes