Browse Source

Working connection

Petra Lamborn 5 years ago
parent
commit
5beb365ef8
1 changed files with 12 additions and 1 deletions
  1. 12
    1
      dbtest.py

+ 12
- 1
dbtest.py View File

1
 import psycopg2 as pg
1
 import psycopg2 as pg
2
 from configparser import ConfigParser
2
 from configparser import ConfigParser
3
+import pandas.io.sql as psql
3
 
4
 
4
 def config(filename='database.ini', section='postgresql'):
5
 def config(filename='database.ini', section='postgresql'):
5
     """Config parser from http://www.postgresqltutorial.com/postgresql-python/connect/"""
6
     """Config parser from http://www.postgresqltutorial.com/postgresql-python/connect/"""
19
  
20
  
20
     return db
21
     return db
21
 
22
 
23
+conn = False
22
 
24
 
23
 try:
25
 try:
24
     params = config()
26
     params = config()
36
     db_version = cur.fetchone()
38
     db_version = cur.fetchone()
37
     print(db_version)
39
     print(db_version)
38
 
40
 
41
+
42
+    # Get tables
43
+    dataframe = psql.read_sql("SELECT * FROM pg_catalog.pg_tables", conn)
44
+
45
+    # Too big!
46
+    # dataframe = psql.read_sql("SELECT * FROM coup_prd.coupdata_2016_06", conn)
47
+
48
+    print(dataframe)
49
+
39
     cur.close()
50
     cur.close()
40
 
51
 
41
 except (Exception, pg.DatabaseError) as error:
52
 except (Exception, pg.DatabaseError) as error:
42
     print(error)
53
     print(error)
43
 finally:
54
 finally:
44
-    if conn is not None:
55
+    if conn is not False:
45
         conn.close()
56
         conn.close()
46
         print('Database connection closed')
57
         print('Database connection closed')