-
Notifications
You must be signed in to change notification settings - Fork 2
/
HandleDb.py
73 lines (62 loc) · 2.55 KB
/
HandleDb.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# -*- coding: utf-8 -*-
"""
/***************************************************************************
XPlan
A QGIS plugin
Fachschale XPlan für XPlanung
-------------------
begin : 2011-03-08
copyright : (C) 2011 by Bernhard Stroebl, KIJ/DV
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 builtins import object
from qgis.PyQt import QtCore, QtSql
from qgis.gui import *
from qgis.core import *
class DbHandler(object):
'''class to handle a QtSql.QSqlDatabase connnection to a PostgreSQL server'''
def __init__(self, iface, tools):
self.iface = iface
self.tools = tools
self.db = None
def dbConnect(self, thisPassword = None):
s = QtCore.QSettings( "XPlanung", "XPlanung-Erweiterung" )
service = ( s.value( "service", "" ) )
host = ( s.value( "host", "" ) )
port = ( s.value( "port", "5432" ) )
database = ( s.value( "dbname", "" ) )
authcfg = s.value( "authcfg", "" )
username, passwd, authcfg = self.tools.getAuthUserNamePassword(authcfg)
if authcfg == None:
username = ( s.value( "uid", "" ) )
passwd = ( s.value( "pwd", "" ) )
if thisPassword:
passwd = thisPassword
# connect to DB
db = QtSql.QSqlDatabase.addDatabase ("QPSQL", "XPlanung")
db.setHostName(host)
db.setPort(int(port))
db.setDatabaseName(database)
db.setUserName(username)
db.setPassword(passwd)
db.authcfg = authcfg # für DDIM
ok2 = db.open()
if not ok2:
self.iface.messageBar().pushMessage("Fehler", \
u"Konnte keine Verbindung mit der Datenbank aufbauen", \
level=Qgis.Critical)
return None
else:
return db
def dbDisconnect(self, db):
db.close()
db = None