def __openDatabase(self, dbPath):
        """

        :type dbPath: str
        :return:
        """
        # QgsMessageLog.logMessage("(VFK) Open DB: {}".format(dbPath))
        if not QSqlDatabase.isDriverAvailable('QSQLITE'):
            raise VFKError(u'Databázový ovladač QSQLITE není dostupný.')

        connectionName = QUuid.createUuid().toString()
        db = QSqlDatabase.addDatabase("QSQLITE", connectionName)
        db.setDatabaseName(dbPath)
        if not db.open():
            raise VFKError(u'Nepodařilo se otevřít databázi. ')

        self.setProperty("connectionName", connectionName)
Exemple #2
0
def open_psql_db(db_host, db_name, db_port, db_admin, db_admin_pwd):
    """
    Open PostGIS db connection taking care that the connection exists only once
    :param db_host: the db host
    :param db_name: the db name
    :param db_port: the db port
    :param db_admin: the db administrator username
    :param db_admin_pwd: the db administrator username password
    :return: a db object
    :rtype: QSqlDatabase
    """

    connection_name = "%s@%s:%s/%s" % (db_admin, db_host, db_port, db_name)
    try:
        if connection_name in QSqlDatabase.connectionNames():
            db = QSqlDatabase.database(connection_name)
        else:
            if not QSqlDatabase.isDriverAvailable("QPSQL"):
                raise VerisoError('Please install the PSQL Qt driver\n'
                                  '(libqt4-sql-psql in ubuntu).\n')

            db = QSqlDatabase.addDatabase("QPSQL", connection_name)
            db.setHostName(db_host)
            db.setPort(int(db_port))
            db.setDatabaseName(db_name)
            db.setUserName(db_admin)
            db.setPassword(db_admin_pwd)

        if not db.open():
            raise Exception()
    except VerisoError:
        raise
    except Exception as e:
        message = "Could not open psql database: %s see log for more details"\
                  % connection_name
        message = tr(message)
        #
        raise VerisoError(message, e, db.lastError().text())
    return db