-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
47 lines (45 loc) · 1.64 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/python
from configparser import ConfigParser
import psycopg2
def config(filename='database.ini', section='postgresql'):
# create a parser
parser = ConfigParser()
# read config file
parser.read(filename)
# get section, default to postgresql
db = {}
if parser.has_section(section):
params = parser.items(section)
for param in params:
db[param[0]] = param[1]
else:
raise Exception('Section {0} not found in the {1} file'.format(section, filename))
return db
def connectPostGreSql():
""" Connect to the PostgreSQL database server """
conn = None
try:
# Ler os parametros de conexão
params = config()
# conecta no PostGreSQL Server
# print('Connecting to the PostgreSQL database...')
conn = psycopg2.connect(**params)
# Cria um cursor
# cur = conn.cursor() Irei criar o cursor apenas na minha função
# @@@@@@ Não estou usando essas instruções abaixo @@@@@@@@
# # Executa uma Declaração
# print('PostgreSQL database version:')
# cur.execute('SELECT version()')
# # display the PostgreSQL database server version
# db_version = cur.fetchone()
# print(db_version)
# # close the communication with the PostgreSQL
# cur.close()
# @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
except (Exception, psycopg2.DatabaseError) as error:
print(error)
# finally:
# if conn is not None:
# conn.close()
# print('Conexão Finalizada/Fechada.')
return conn