-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdbconn.py
49 lines (41 loc) · 1.91 KB
/
dbconn.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
48
49
"""
/***************************************************************************
QGIS Historize Plugin
-------------------------------------------------------------------
Date : 09 Mai 2017
Copyright : (C) 2017 by William Habelt
email : [email protected]
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qgis.core import *
import psycopg2
class DBConn:
"""Class for establishing a DB-connection"""
def __init__(self, iface):
self.iface = iface
def connectToDb(self, uri):
"""Create a connection object from a uri and return it."""
# conninfo = uri.connectionInfo()
conn = None
ok = False
while not conn:
try:
conn = psycopg2.connect(uri.connectionInfo())
except psycopg2.OperationalError as e:
(ok, user, passwd) = QgsCredentials.instance().get(uri.connectionInfo(), uri.username(), uri.password())
if not ok:
break
if not conn:
QMessageBox.warning(self.iface.mainWindow(), self.tr(u"Connection Error"), self.tr(u"Could not connect to PostgreSQL database - check connection."))
if ok:
QgsCredentials.instance().put(uri.connectionInfo(), user, passwd)
return conn