DatabaseExporter

Description

The DatabaseExporter object is used to export data from a database to a DataObjectWriter for example a CSV-exporter. The query property is used to select, limit and format the output.

› Inherits:Object

Properties

chunkSize

This property holds the number of datasets to export in each internal iteration. Reducing this value improves the responsiveness but decreases the performance. When a custom query is used, this value is not used and all datasets are exported at once.

This property was introduced in InCore 2.1.

› Type:SignedInteger
› Default:1000
› Signal:chunkSizeChanged()
› Attributes:Writable

dataFormat

This property holds whether the data should be formatted according to the object (for example a Measurement would be printed with unit) or be left unformatted (the value is printed as string).

› Type:DataFormat
› Default:DatabaseExporter.RawValues
› Signal:dataFormatChanged()
› Attributes:Writable

error

This property holds the most recently occurred error or DatabaseExporter.NoError if no error occurred. If the same error occurs multiple times this property does not change. Use the errorOccurred() signal to detect multiple occurrences of the same error.

› Type:Error
› Signal:errorChanged()
› Attributes:Readonly

errorString

This property holds the current human readable error string corresponding to the current value in the error property. It may include additional information such as failure reasons or locations.

› Type:String
› Signal:errorStringChanged()
› Attributes:Readonly

output

This property holds the DataObjectWriter to output the data. The format and location of the data is defined by the output.

› Type:DataObjectWriter
› Signal:outputChanged()
› Attributes:Writable

progress

This property holds the overall progress of the current export operatoin

This property was introduced in InCore 2.1.

› Type:SignedInteger
› Default:0
› Signal:progressChanged()
› Attributes:Readonly

query

This property holds the query which defines which data should be exported. If left blank the whole table is exported.

› Type:DatabaseQuery
› Signal:queryChanged()
› Attributes:Writable, Optional

rowFilter

This property holds an expression which is used to filter rows when query can’t be used. The respective data row is provided in the item variable. Therefore an expression looks like item.myField > 123 or item.userId !== undefined.

This property was introduced in InCore 2.1.

› Type:<QML expression>
› Signal:rowFilterChanged()
› Attributes:Writable

running

This property holds whether an export operation is in progress.

This property was introduced in InCore 2.1.

› Type:Boolean
› Default:false
› Signal:runningChanged()
› Attributes:Writable

table

This property holds the table which data should be exported. If left blank the table of the query property is used. If query is left blank or you want to export the whole table you have to set this property properly.

› Type:DatabaseTable
› Signal:tableChanged()
› Attributes:Writable

Methods

execute()

This method checks for errors and starts an export operation. The finished() signal will be emitted after execution has finished.

› Returns:Boolean

Signals

errorOccurred()

This signal is emitted whenever an error has occurred, regardless of whether the error property has changed or not. In contrast to the change notification signal of the error property this signal is also emitted several times if a certain error occurs several times in succession.

finished()

This signal is emitted after the export has been finished. Check the error property or connect to the errorOccurred() signal to detect errors occurred during the export.

This signal was introduced in InCore 2.1.

Enumerations

DataFormat

This enumeration describes the ways the data can be formatted.

Name Value Description
DatabaseExporter.RawValues 0 The raw value is converted to a string.
DatabaseExporter.FormattedData 1 The value is formatted by the corresponding object according to the configured settings (for example Measurement may add the unit, format the floating point value with the configured number of decimals and apply the SI prefix, e.g. 123456“1234.5 kPa”).

Error

This enumeration describes all errors which can occur in DatabaseExporter objects. The most recently occurred error is stored in the error property.

Name Value Description
DatabaseExporter.NoError 0 No error occurred or was detected.
DatabaseExporter.InvalidOutputError 1 Invalid or no output set.
DatabaseExporter.InvalidTableError 2 No table set in query or exporter or table not open.
DatabaseExporter.DataError 3 Internal error while fetching data.
DatabaseExporter.OutputOpenError 4 Opening the output caused an error.
DatabaseExporter.WriteError 5 Internal error while exporting data to file.

Example

import InCore.Foundation 2.5
import InCore.Database 2.5

Application {

    LocalDatabase {
        id: exampleDatabase
        onErrorChanged: console.log("LocalDatabase error:", errorString)

        DatabaseTable {
            id: exampleTable
            onErrorChanged: console.log("DatabaseTable error:", errorString)

            DateTime { id: date }
            Measurement { id: sensor1; unit: "°C"; decimals: 1 }
            Measurement { id: sensor2; unit: "Pa"; decimals: 2; siPrefix: Measurement.Kilo }
        }
    }

    DatabaseExporter {
        id: dbExporter
        onErrorChanged: console.log("Export error:", errorString)
        table: exampleTable
        dataFormat: DatabaseExporter.FormattedData
        output: CsvWriter {
            output: File {
                fileName: "SensorValues.csv"
                storage: UsbStorage { }
            }
        }
    }

    Timer {
        onTriggered: {
            sensor1.data = Math.random() * 100
            sensor2.data = 1000 + Math.random() * 100
            exampleTable.submit();
        }
    }

    Timer {
        interval: 10*1000
        onTriggered: {
            dbExporter.execute();
            exampleTable.truncate()
        }
    }
}