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,5 +1,6 @@
1 1
 import psycopg2 as pg
2 2
 from configparser import ConfigParser
3
+import pandas.io.sql as psql
3 4
 
4 5
 def config(filename='database.ini', section='postgresql'):
5 6
     """Config parser from http://www.postgresqltutorial.com/postgresql-python/connect/"""
@@ -19,6 +20,7 @@ def config(filename='database.ini', section='postgresql'):
19 20
  
20 21
     return db
21 22
 
23
+conn = False
22 24
 
23 25
 try:
24 26
     params = config()
@@ -36,11 +38,20 @@ try:
36 38
     db_version = cur.fetchone()
37 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 50
     cur.close()
40 51
 
41 52
 except (Exception, pg.DatabaseError) as error:
42 53
     print(error)
43 54
 finally:
44
-    if conn is not None:
55
+    if conn is not False:
45 56
         conn.close()
46 57
         print('Database connection closed')