Пример #1
0
    def sqlExec(self, sql):
        # Execute a SQL query and, return [header, data, rowCount]
        connector = PostGisDBConnector(self.uri)

        #print "DEBUG dbrequest : sql = " + sql

        try:
            c = connector._execute(None, unicode(sql))
            data = []
            header = connector._get_cursor_columns(c)
        except:
            print "Erreur SQL : " + str(sql)  # debug purpose
            raise

        if header is None:
            header = []

        if len(header) > 0:
            data = connector._fetchall(c)

        row_count = c.rowcount
        if row_count == -1:
            row_count = len(data)

        if c:
            c.close()
            del c

        connector.__del__()

        return [header, data, row_count]
Пример #2
0
def fetchDataFromSqlQuery(connection_name, sql):
    data = []
    header = []
    rowCount = 0

    # Get URI
    status, uri, error_message = getUriFromConnectionName(
        connection_name, True)
    if not uri or not status:
        ok = False
        return header, data, rowCount, ok, error_message
    try:
        connector = PostGisDBConnector(uri)
    except Exception:
        error_message = tr('Cannot connect to database')
        ok = False
        return header, data, rowCount, ok, error_message

    c = None
    ok = True
    # print "run query"
    try:
        c = connector._execute(None, str(sql))
        data = []
        header = connector._get_cursor_columns(c)
        if header is None:
            header = []
        if len(header) > 0:
            data = connector._fetchall(c)
        rowCount = c.rowcount
        if rowCount == -1:
            rowCount = len(data)

    except BaseError as e:
        ok = False
        error_message = e.msg
        return header, data, rowCount, ok, error_message
    finally:
        if c:
            c.close()
            del c

    # Log errors
    if not ok:
        error_message = tr('Unknown error occurred while fetching data')
        return header, data, rowCount, ok, error_message

    return header, data, rowCount, ok, error_message
Пример #3
0
def fetchDataFromSqlQuery(connection_name, sql):
    from db_manager.db_plugins.plugin import BaseError
    from db_manager.db_plugins import createDbPlugin
    from db_manager.db_plugins.postgis.connector import PostGisDBConnector

    header = None
    data = []
    header = []
    rowCount = 0
    error_message = None
    connection = None

    # Create plugin class and try to connect
    ok = True
    try:
        dbpluginclass = createDbPlugin('postgis', connection_name)
        connection = dbpluginclass.connect()
    except BaseError as e:
        # DlgDbError.showError(e, self.dialog)
        ok = False
        error_message = e.msg
    except Exception:
        ok = False
        error_message = 'Connexion à la base de données impossible'

    if not connection:
        return [header, data, rowCount, ok, error_message]

    db = dbpluginclass.database()
    if not db:
        ok = False
        error_message = 'Impossible de récupérer la base de données depuis la connexion'
        return [header, data, rowCount, ok, error_message]

    # Get URI
    uri = db.uri()
    try:
        connector = PostGisDBConnector(uri)
    except Exception:
        error_message = 'Impossible de se connecer à la base de données'
        ok = False
        return [header, data, rowCount, ok, error_message]

    c = None
    ok = True
    # print "run query"
    try:
        c = connector._execute(None, str(sql))
        data = []
        header = connector._get_cursor_columns(c)
        if header is None:
            header = []
        if len(header) > 0:
            data = connector._fetchall(c)
        rowCount = c.rowcount
        if rowCount == -1:
            rowCount = len(data)

    except BaseError as e:
        ok = False
        error_message = e.msg
        return [header, data, rowCount, ok, error_message]
    finally:
        if c:
            c.close()
            del c

    # Log errors
    if not ok:
        error_message = 'Une erreur inconnue lors de la récupération des données'
        return [header, data, rowCount, ok, error_message]
        print(error_message)
        print(sql)

    return [header, data, rowCount, ok, error_message]