1 | #!/usr/bin/env python |
---|
2 | # -*- coding: utf-8 -*- |
---|
3 | |
---|
4 | # ==================================================================== # |
---|
5 | # Author: Sonia Labetoulle # |
---|
6 | # Contact: sonia.labetoulle _at_ ipsl.jussieu.fr # |
---|
7 | # Created: 2016 # |
---|
8 | # History: # |
---|
9 | # Modification: # |
---|
10 | # ==================================================================== # |
---|
11 | |
---|
12 | # =================================================================== # |
---|
13 | # ssh readonly@prodiguer-test-db.ipsl.upmc.fr # |
---|
14 | # psql -U prodiguer_db_user prodiguer # |
---|
15 | # # |
---|
16 | # ssh readonly@prodiguer-test-db.ipsl.upmc.fr -L 5432:localhost:5432 # |
---|
17 | # =================================================================== # |
---|
18 | |
---|
19 | |
---|
20 | # This must come first |
---|
21 | # from __future__ import print_function, unicode_literals, division |
---|
22 | from __future__ import print_function, division |
---|
23 | |
---|
24 | # Standard library imports |
---|
25 | import psycopg2 |
---|
26 | import psycopg2.extras |
---|
27 | |
---|
28 | # Application library imports |
---|
29 | |
---|
30 | |
---|
31 | ####################################################################### |
---|
32 | def connect_db(db_host, db_name, db_user, db_pwd): |
---|
33 | |
---|
34 | conn_string = "host={} dbname={} user={} password={}".format( |
---|
35 | db_host, db_name, db_user, db_pwd |
---|
36 | ) |
---|
37 | |
---|
38 | # Print the connection string we will use to connect |
---|
39 | print("Connecting to database\n -> {}".format( |
---|
40 | conn_string.replace(db_pwd, "*****") |
---|
41 | ) |
---|
42 | ) |
---|
43 | |
---|
44 | # Get a connection, if a connect cannot be made an exception will be |
---|
45 | # raised here |
---|
46 | try: |
---|
47 | conn = psycopg2.connect(conn_string) |
---|
48 | except Exception as rc: |
---|
49 | print("Problem with DB connect:\n{}".format(rc)) |
---|
50 | exit(9) |
---|
51 | |
---|
52 | # conn.cursor will return a cursor object, you can use this cursor |
---|
53 | # to perform queries |
---|
54 | # cursor = conn.cursor() |
---|
55 | cursor = conn.cursor(cursor_factory=psycopg2.extras.DictCursor) |
---|
56 | print(" => Connected!\n") |
---|
57 | |
---|
58 | return conn, cursor |
---|
59 | |
---|
60 | |
---|
61 | ####################################################################### |
---|
62 | def select_db(cursor, request): |
---|
63 | |
---|
64 | # Execute query |
---|
65 | try: |
---|
66 | cursor.execute(request) |
---|
67 | except Exception as rc: |
---|
68 | print("Problem with select:\n{}".format(rc)) |
---|
69 | exit(9) |
---|
70 | |
---|
71 | # # retrieve the records from the database |
---|
72 | # records = cursor.fetchall() |
---|
73 | |
---|
74 | # # print out the records using pretty print |
---|
75 | # # note that the NAMES of the columns are not shown, instead just indexes. |
---|
76 | # # for most people this isn't very useful so we'll show you how to return |
---|
77 | # # columns as a dictionary (hash) in the next example. |
---|
78 | # pprint(records) |
---|
79 | |
---|
80 | |
---|
81 | ####################################################################### |
---|
82 | def insert_db(cursor, request): |
---|
83 | """ |
---|
84 | """ |
---|
85 | |
---|
86 | # Execute query |
---|
87 | try: |
---|
88 | cursor.execute(request) |
---|
89 | except Exception as rc: |
---|
90 | print("Problem with insert:\n{}".format(rc)) |
---|
91 | exit(9) |
---|
92 | |
---|
93 | |
---|
94 | ####################################################################### |
---|
95 | def commit_db(conn): |
---|
96 | """ |
---|
97 | """ |
---|
98 | |
---|
99 | # Execute query |
---|
100 | try: |
---|
101 | conn.commit() |
---|
102 | except Exception as rc: |
---|
103 | print("Problem with commit:\n{}".format(rc)) |
---|
104 | exit(9) |
---|
105 | |
---|
106 | |
---|
107 | ####################################################################### |
---|
108 | def close_db(conn): |
---|
109 | """ |
---|
110 | """ |
---|
111 | |
---|
112 | try: |
---|
113 | conn.close() |
---|
114 | except Exception as rc: |
---|
115 | print("Problem with close:\n{}".format(rc)) |
---|
116 | exit(9) |
---|
117 | |
---|
118 | |
---|
119 | ####################################################################### |
---|
120 | if __name__ == "__main__": |
---|
121 | pass |
---|