Class: QgsAbstractDatabaseProviderConnection

class qgis.core.QgsAbstractDatabaseProviderConnection(name: str)

Bases: QgsAbstractProviderConnection

Creates a new connection with name by reading its configuration from the settings. If a connection with this name cannot be found, an empty connection will be returned.

QgsAbstractDatabaseProviderConnection(uri: str, configuration: Dict[str, Any]) Creates a new connection from the given uri and configuration. The connection is not automatically stored in the settings.

See also

store()

QgsAbstractDatabaseProviderConnection(QgsAbstractDatabaseProviderConnection)

The QgsAbstractDatabaseProviderConnection class provides common functionality for DB based connections.

This class performs low level DB operations without asking the user for confirmation or handling currently opened layers and the registry entries, it is responsibility of the client code to keep layers in sync. The class methods will throw exceptions in case the requested operation is not supported or cannot be performed without errors.

New in version 3.10.

Parameters

name

AddField = 2097152
Aspatial = 2
class Capabilities

Bases: sip.wrapper

QgsAbstractDatabaseProviderConnection.Capabilities(Union[QgsAbstractDatabaseProviderConnection.Capabilities, QgsAbstractDatabaseProviderConnection.Capability]) QgsAbstractDatabaseProviderConnection.Capabilities(QgsAbstractDatabaseProviderConnection.Capabilities)

baseClass

alias of QgsAbstractDatabaseProviderConnection

class Capability

Bases: int

baseClass

alias of QgsAbstractDatabaseProviderConnection

CreateSchema = 64
CreateSpatialIndex = 65536
CreateVectorTable = 2
Curves = 16
DeleteField = 524288
DeleteFieldCascade = 1048576
DeleteSpatialIndex = 262144
DropRasterTable = 4
DropSchema = 128
DropVectorTable = 8
ExecuteSql = 512
Foreign = 64
class GeometryColumnCapabilities

Bases: sip.wrapper

QgsAbstractDatabaseProviderConnection.GeometryColumnCapabilities(Union[QgsAbstractDatabaseProviderConnection.GeometryColumnCapabilities, QgsAbstractDatabaseProviderConnection.GeometryColumnCapability]) QgsAbstractDatabaseProviderConnection.GeometryColumnCapabilities(QgsAbstractDatabaseProviderConnection.GeometryColumnCapabilities)

baseClass

alias of QgsAbstractDatabaseProviderConnection

class GeometryColumnCapability

Bases: int

baseClass

alias of QgsAbstractDatabaseProviderConnection

M = 4
MaterializedView = 32
Raster = 8
RenameRasterTable = 32
RenameSchema = 256
RenameVectorTable = 16
Schemas = 4096
SinglePart = 8
Spatial = 32768
SpatialIndexExists = 131072
class SpatialIndexOptions

Bases: sip.wrapper

QgsAbstractDatabaseProviderConnection.SpatialIndexOptions(QgsAbstractDatabaseProviderConnection.SpatialIndexOptions)

geometryColumnName
SqlLayers = 8192
TableExists = 16384
class TableFlag

Bases: int

class TableFlags

Bases: sip.wrapper

QgsAbstractDatabaseProviderConnection.TableFlags(Union[QgsAbstractDatabaseProviderConnection.TableFlags, QgsAbstractDatabaseProviderConnection.TableFlag]) QgsAbstractDatabaseProviderConnection.TableFlags(QgsAbstractDatabaseProviderConnection.TableFlags)

baseClass

alias of QgsAbstractDatabaseProviderConnection

class TableProperty

Bases: sip.wrapper

QgsAbstractDatabaseProviderConnection.TableProperty(QgsAbstractDatabaseProviderConnection.TableProperty)

class GeometryColumnType

Bases: sip.wrapper

QgsAbstractDatabaseProviderConnection.TableProperty.GeometryColumnType(QgsAbstractDatabaseProviderConnection.TableProperty.GeometryColumnType)

crs
wkbType
addGeometryColumnType(self, type: QgsWkbTypes.Type, crs: QgsCoordinateReferenceSystem)

Appends the geometry column type with the given srid to the geometry column types list

Parameters
at(self, index: int)QgsAbstractDatabaseProviderConnection.TableProperty

Returns the table property corresponding to the geometry type at the given index

Parameters

index (int) –

Return type

QgsAbstractDatabaseProviderConnection.TableProperty

comment(self) → str

Returns the table comment

Return type

str

crsList(self) → List[QgsCoordinateReferenceSystem]

Returns the list of CRSs supported by the geometry column

Return type

List[QgsCoordinateReferenceSystem]

defaultName(self) → str

Returns the default name for the table entry

It is usually the table name but in case there are multiple geometry columns, the geometry column name is appended to the table name.

Return type

str

flags(self)QgsAbstractDatabaseProviderConnection.TableFlags

Returns the table flags

Return type

QgsAbstractDatabaseProviderConnection.TableFlags

geometryColumn(self) → str

Returns the geometry column name

Return type

str

geometryColumnCount(self) → int

Returns the number of geometry columns in the original table this entry refers to

This information is used internally to build the defaultName()

Return type

int

geometryColumnTypes(self) → List[QgsAbstractDatabaseProviderConnection.TableProperty.GeometryColumnType]

Returns the list of geometry column types and CRSs. The method returns a list of GeometryColumnType

Return type

List[QgsAbstractDatabaseProviderConnection.TableProperty.GeometryColumnType]

info(self) → Dict[str, Any]

Returns additional information about the table

Provider classes may use this property to store custom bits of information.

Return type

Dict[str, Any]

maxCoordinateDimensions(self) → int

Returns the maximum coordinate dimensions of the geometries of a vector table. This information is calculated from the geometry columns types.

Return type

int

primaryKeyColumns(self) → List[str]

Returns the list of primary key column names

Return type

List[str]

schema(self) → str

Returns the schema or an empty string for backends that do not support a schema

Return type

str

setComment(self, comment: str)

Sets the table comment

Parameters

comment (str) –

setFlag(self, flag: QgsAbstractDatabaseProviderConnection.TableFlag)

Sets a flag

Parameters

flag (QgsAbstractDatabaseProviderConnection.TableFlag) –

setFlags(self, flags: Union[QgsAbstractDatabaseProviderConnection.TableFlags, QgsAbstractDatabaseProviderConnection.TableFlag])

Sets the table flags

Parameters

flags (Union[QgsAbstractDatabaseProviderConnection.TableFlags) –

setGeometryColumn(self, geometryColumn: str)

Sets the geometry column name to geometryColumn

Parameters

geometryColumn (str) –

setGeometryColumnCount(self, geometryColumnCount: int)

Sets the geometryColumnCount

Parameters

geometryColumnCount (int) –

setGeometryColumnTypes(self, geometryColumnTypes: Iterable[QgsAbstractDatabaseProviderConnection.TableProperty.GeometryColumnType])

Sets the geometry column types to geometryColumnTypes

Parameters

geometryColumnTypes (Iterable[QgsAbstractDatabaseProviderConnection.TableProperty.GeometryColumnType]) –

setInfo(self, info: Dict[str, Any])

Sets additional information about the table to info

Provider classes may use this property to store custom bits of information.

Parameters

info (Dict[str) –

setPrimaryKeyColumns(self, primaryKeyColumns: Iterable[str])

Sets the primary key column names to primaryKeyColumns

Parameters

primaryKeyColumns (Iterable[str]) –

setSchema(self, schema: str)

Sets the schema

Parameters

schema (str) –

setTableName(self, name: str)

Sets the table name to name

See also

defaultName()

Parameters

name (str) –

tableName(self) → str

Returns the table name

See also

defaultName()

Return type

str

Tables = 2048
Vacuum = 1024
Vector = 4
View = 16
Z = 2
addField(self, field: QgsField, schema: str, tableName: str)

Adds a field Raises a QgsProviderConnectionException if any errors are encountered.

Parameters
  • field (QgsField) – specification of the new field

  • schema (str) – name of the schema (schema is ignored if not supported by the backend).

  • tableName (str) – name of the table

Note

it is responsibility of the caller to handle open layers and registry entries.

Raises

: – py:class:QgsProviderConnectionException

New in version 3.16.

capabilities(self)QgsAbstractDatabaseProviderConnection.Capabilities

Returns connection capabilities

Return type

QgsAbstractDatabaseProviderConnection.Capabilities

checkCapability(self, capability: QgsAbstractDatabaseProviderConnection.Capability)

Checks if capability is supported and throws and exception if it’s not

Raises

: – py:class:QgsProviderConnectionException

Parameters

capability (QgsAbstractDatabaseProviderConnection.Capability) –

createSchema(self, name: str)

Creates a new schema with the specified name

Raises

: – py:class:QgsProviderConnectionException

Parameters

name (str) –

createSpatialIndex(self, schema: str, name: str, options: QgsAbstractDatabaseProviderConnection.SpatialIndexOptions = QgsAbstractDatabaseProviderConnection.SpatialIndexOptions())

Creates a spatial index for the database table with given schema and name (schema is ignored if not supported by the backend).

The options argument can be used to provide extra options controlling the spatial index creation.

Raises a QgsProviderConnectionException if any errors are encountered.

Raises

: – py:class:QgsProviderConnectionException

New in version 3.14.

Parameters
  • schema (str) –

  • name (str) –

  • options (QgsAbstractDatabaseProviderConnection.SpatialIndexOptions = QgsAbstractDatabaseProviderConnection.SpatialIndexOptions()) –

createVectorTable(self, schema: str, name: str, fields: QgsFields, wkbType: QgsWkbTypes.Type, srs: QgsCoordinateReferenceSystem, overwrite: bool, options: Dict[str, Any])

Creates an empty table with name in the given schema (schema is ignored if not supported by the backend). Raises a QgsProviderConnectionException if any errors are encountered.

Raises

: – py:class:QgsProviderConnectionException

Parameters
deleteField(self, fieldName: str, schema: str, tableName: str, force: bool = False)

Deletes the field with the specified name. Raises a QgsProviderConnectionException if any errors are encountered.

Parameters
  • fieldName (str) – name of the field to be deleted

  • schema (str) – name of the schema (schema is ignored if not supported by the backend).

  • tableName (str) – name of the table

  • force (bool = False) – if True, a DROP CASCADE will drop all related objects

Note

it is responsibility of the caller to handle open layers and registry entries.

Raises

: – py:class:QgsProviderConnectionException

New in version 3.16.

deleteSpatialIndex(self, schema: str, name: str, geometryColumn: str)

Deletes the existing spatial index for the database table with given schema, name and geometryColumn (schema and geometryColumn are ignored if not supported by the backend).

Raises a QgsProviderConnectionException if any errors are encountered.

Raises

: – py:class:QgsProviderConnectionException

New in version 3.14.

Parameters
  • schema (str) –

  • name (str) –

  • geometryColumn (str) –

dropRasterTable(self, schema: str, name: str)

Drops a raster table with given schema (schema is ignored if not supported by the backend) and name. Raises a QgsProviderConnectionException if any errors are encountered.

Note

it is responsibility of the caller to handle open layers and registry entries.

Raises

: – py:class:QgsProviderConnectionException

Parameters
  • schema (str) –

  • name (str) –

dropSchema(self, name: str, force: bool = False)

Drops an entire schema with the specified name. Raises a QgsProviderConnectionException if any errors are encountered.

Parameters
  • name (str) – name of the schema to be dropped

  • force (bool = False) – if True, a DROP CASCADE will drop all related objects

Note

it is responsibility of the caller to handle open layers and registry entries.

Raises

: – py:class:QgsProviderConnectionException

dropVectorTable(self, schema: str, name: str)

Drops a vector (or aspatial) table with given schema (schema is ignored if not supported by the backend) and name. Raises a QgsProviderConnectionException if any errors are encountered.

Note

it is responsibility of the caller to handle open layers and registry entries.

Raises

: – py:class:QgsProviderConnectionException

Parameters
  • schema (str) –

  • name (str) –

executeSql(self, sql: str, feedback: QgsFeedback = None) → object

Executes raw sql and returns the (possibly empty) list of results in a multi-dimensional array, optionally feedback can be provided. Raises a QgsProviderConnectionException if any errors are encountered.

Raises

: – py:class:QgsProviderConnectionException

Parameters
Return type

object

fields(self, schema: str, table: str)QgsFields

Returns the fields of a table and schema. Raises a QgsProviderConnectionException if any errors are encountered.

Note

the default implementation creates a temporary vector layer, providers may choose to override this method for a greater efficiency.

Raises

: – py:class:QgsProviderConnectionException

New in version 3.16.

Parameters
  • schema (str) –

  • table (str) –

Return type

QgsFields

geometryColumnCapabilities(self)QgsAbstractDatabaseProviderConnection.GeometryColumnCapabilities

Returns connection geomerty column capabilities (Z, M, SinglePart, Curves)

New in version 3.16.

Return type

QgsAbstractDatabaseProviderConnection.GeometryColumnCapabilities

nativeTypes(self) → List[QgsVectorDataProvider.NativeType]

Returns a list of native types supported by the connection.

Raises

: – py:class:QgsProviderConnectionException

New in version 3.16.

Return type

List[QgsVectorDataProvider.NativeType]

providerKey(self) → str

Returns the provider key

New in version 3.16.

Return type

str

renameRasterTable(self, schema: str, name: str, newName: str)

Renames a raster table with given schema (schema is ignored if not supported by the backend) and name. Raises a QgsProviderConnectionException if any errors are encountered.

Note

it is responsibility of the caller to handle open layers and registry entries.

Raises

: – py:class:QgsProviderConnectionException

Parameters
  • schema (str) –

  • name (str) –

  • newName (str) –

renameSchema(self, name: str, newName: str)

Renames a schema with the specified name. Raises a QgsProviderConnectionException if any errors are encountered.

Note

it is responsibility of the caller to handle open layers and registry entries.

Raises

: – py:class:QgsProviderConnectionException

Parameters
  • name (str) –

  • newName (str) –

renameVectorTable(self, schema: str, name: str, newName: str)

Renames a vector or aspatial table with given schema (schema is ignored if not supported by the backend) and name. Raises a QgsProviderConnectionException if any errors are encountered.

Note

it is responsibility of the caller to handle open layers and registry entries.

Raises

: – py:class:QgsProviderConnectionException

Parameters
  • schema (str) –

  • name (str) –

  • newName (str) –

schemas(self) → List[str]

Returns information about the existing schemas. Raises a QgsProviderConnectionException if any errors are encountered.

Raises

: – py:class:QgsProviderConnectionException

Return type

List[str]

spatialIndexExists(self, schema: str, name: str, geometryColumn: str) → bool

Determines whether a spatial index exists for the database table with given schema, name and geometryColumn (schema and geometryColumn are ignored if not supported by the backend).

Raises a QgsProviderConnectionException if any errors are encountered.

Raises

: – py:class:QgsProviderConnectionException

New in version 3.14.

Parameters
  • schema (str) –

  • name (str) –

  • geometryColumn (str) –

Return type

bool

staticMetaObject = <PyQt5.QtCore.QMetaObject object>
table(self, schema: str, table: str)QgsAbstractDatabaseProviderConnection.TableProperty

Returns information on a table in the given schema. Raises a QgsProviderConnectionException if any errors are encountered or if the table does not exist.

Raises

: – py:class:QgsProviderConnectionException

Note

Not available in Python bindings

New in version 3.12.

Parameters
  • schema (str) –

  • table (str) –

Return type

QgsAbstractDatabaseProviderConnection.TableProperty

tableExists(self, schema: str, name: str) → bool

Checks whether a table name exists in the given schema. Raises a QgsProviderConnectionException if any errors are encountered.

Raises

: – py:class:QgsProviderConnectionException

Parameters
  • schema (str) –

  • name (str) –

Return type

bool

tableUri(self, schema: str, name: str) → str

Returns the URI string for the given table and schema. Raises a QgsProviderConnectionException if any errors are encountered.

Raises

: – py:class:QgsProviderConnectionException

New in version 3.12.

Parameters
  • schema (str) –

  • name (str) –

Return type

str

tables(self, schema: str = '', flags: int = 0) → List[QgsAbstractDatabaseProviderConnection.TableProperty]

Returns information on the tables in the given schema. Raises a QgsProviderConnectionException if any errors are encountered.

Parameters
  • schema (str = '') – name of the schema (ignored if not supported by the backend)

  • flags (int = 0) – filter tables by flags, this option completely overrides search options stored in the connection

Raises

: – py:class:QgsProviderConnectionException

Return type

List[QgsAbstractDatabaseProviderConnection.TableProperty]

vacuum(self, schema: str, name: str)

Vacuum the database table with given schema and name (schema is ignored if not supported by the backend). Raises a QgsProviderConnectionException if any errors are encountered.

Raises

: – py:class:QgsProviderConnectionException

Parameters
  • schema (str) –

  • name (str) –