Class: QgsDataSourceUri

Stores the component parts of a data source URI (e.g. a Postgres data source).

This structure stores the database connection information, including host, database, user name, password, schema, password, and SQL where clause.

Methods

authConfigId

Returns any associated authentication configuration ID stored in the URI.

clearSchema

Clears the schema stored in the URI.

connectionInfo

Returns the connection part of the URI.

database

Returns the database name stored in the URI.

disableSelectAtId

Set to True to disable selection by feature ID.

driver

Returns the driver name stored in the URI

encodedUri

Returns the complete encoded URI as a byte array.

geometryColumn

Returns the name of the geometry column stored in the URI, if set.

hasParam

Returns True if a parameter with the specified key exists.

host

Returns the host name stored in the URI.

httpHeader

Returns the http header value according to key

httpHeaders

Returns http headers

keyColumn

Returns the name of the (primary) key column for the referenced table.

param

Returns a generic parameter value corresponding to the specified key.

parameterKeys

Returns parameter keys used in the uri: specialized ones ("table", "schema", etc.) or generic parameters.

params

Returns multiple generic parameter values corresponding to the specified key.

password

Returns the password stored in the URI.

port

Returns the port stored in the URI.

quotedTablename

Returns the URI's table name, escaped and quoted.

removeParam

Removes a generic parameter by key.

schema

Returns the schema stored in the URI.

selectAtIdDisabled

Returns whether the selection by feature ID is disabled.

service

Returns the service name associated with the URI.

setAuthConfigId

Sets the authentication configuration ID for the URI.

setConnection

Sets all connection related members at once.

setDataSource

Sets all data source related members at once.

setDatabase

Sets the URI database name.

setDriver

Sets the driver name stored in the URI.

setEncodedUri

Sets the complete encoded uri from a string value.

setGeometryColumn

Sets geometry column name to geometryColumn

setHost

Sets the host name stored in the URI.

setHttpHeaders

Sets headers to headers

setKeyColumn

Sets the name of the (primary) key column.

setParam

Sets a generic parameter value on the URI.

setParamList

Sets a generic parameter list value on the URI.

setPassword

Sets the password for the URI.

setPort

Sets the port stored in the URI.

setSchema

Sets the scheme for the URI.

setService

Sets the service name associated with the URI.

setSql

Sets the sql filter for the URI.

setSrid

Sets the spatial reference ID associated with the URI.

setSslMode

Sets the SSL mode associated with the URI.

setTable

Sets table to table

setUseEstimatedMetadata

Sets whether estimated metadata should be used for the connection.

setUsername

Sets the username for the URI.

setWkbType

Sets the WKB type associated with the URI.

sql

Returns the SQL filter stored in the URI, if set.

srid

Returns the spatial reference ID associated with the URI.

sslMode

Returns the SSL mode associated with the URI.

table

Returns the table name stored in the URI.

uri

Returns the complete URI as a string.

useEstimatedMetadata

Returns True if estimated metadata should be used for the connection.

username

Returns the username stored in the URI.

wkbType

Returns the WKB type associated with the URI.

Static Methods

decodeSslMode

Decodes SSL mode string into enum value.

encodeSslMode

Encodes SSL mode enum value into a string.

removePassword

Removes the password element from a URI.

Attributes

SslAllow

SslDisable

SslPrefer

SslRequire

SslVerifyCa

SslVerifyFull

class qgis.core.QgsDataSourceUri[source]

Bases: object

__init__()
__init__(uri: str | None)

Constructor for QgsDataSourceUri which parses an input uri string.

Parameters:

uri (Optional[str])

__init__(a0: QgsDataSourceUri)
Parameters:

a0 (QgsDataSourceUri)

SslAllow = 2
SslDisable = 1
class SslMode

Bases: int

SslPrefer = 0
SslRequire = 3
SslVerifyCa = 4
SslVerifyFull = 5
authConfigId(self) str[source]

Returns any associated authentication configuration ID stored in the URI.

Return type:

str

clearSchema(self)[source]

Clears the schema stored in the URI.

connectionInfo(self, expandAuthConfig: bool = True) str[source]

Returns the connection part of the URI.

Parameters:

expandAuthConfig (bool = True)

Return type:

str

database(self) str[source]

Returns the database name stored in the URI.

Return type:

str

static decodeSslMode(sslMode: str | None) QgsDataSourceUri.SslMode[source]

Decodes SSL mode string into enum value. If the string is not recognized, SslPrefer is returned.

Added in version 3.2.

Parameters:

sslMode (Optional[str])

Return type:

QgsDataSourceUri.SslMode

disableSelectAtId(self, flag: bool)[source]

Set to True to disable selection by feature ID.

Parameters:

flag (bool)

driver(self) str[source]

Returns the driver name stored in the URI

Return type:

str

static encodeSslMode(sslMode: QgsDataSourceUri.SslMode) str[source]

Encodes SSL mode enum value into a string.

Added in version 3.2.

Parameters:

sslMode (QgsDataSourceUri.SslMode)

Return type:

str

encodedUri(self) QByteArray[source]

Returns the complete encoded URI as a byte array.

Return type:

QByteArray

geometryColumn(self) str[source]

Returns the name of the geometry column stored in the URI, if set.

Return type:

str

hasParam(self, key: str | None) bool[source]

Returns True if a parameter with the specified key exists.

Parameters:

key (Optional[str])

Return type:

bool

host(self) str[source]

Returns the host name stored in the URI.

See also

setHost()

Return type:

str

httpHeader(self, key: str | None) str[source]

Returns the http header value according to key

Added in version 3.26.

Parameters:

key (Optional[str])

Return type:

str

httpHeaders(self) QgsHttpHeaders

Returns http headers

Added in version 3.26.

Return type:

QgsHttpHeaders

keyColumn(self) str[source]

Returns the name of the (primary) key column for the referenced table.

Return type:

str

param(self, key: str | None) str[source]

Returns a generic parameter value corresponding to the specified key.

Parameters:

key (Optional[str])

Return type:

str

parameterKeys(self) Set[str]

Returns parameter keys used in the uri: specialized ones (“table”, “schema”, etc.) or generic parameters.

Added in version 3.26.

Return type:

Set[str]

params(self, key: str | None) List[str][source]

Returns multiple generic parameter values corresponding to the specified key.

Parameters:

key (Optional[str])

Return type:

List[str]

password(self) str[source]

Returns the password stored in the URI.

Return type:

str

port(self) str[source]

Returns the port stored in the URI.

See also

setPort()

Return type:

str

quotedTablename(self) str[source]

Returns the URI’s table name, escaped and quoted.

Return type:

str

removeParam(self, key: str | None) int[source]

Removes a generic parameter by key.

Note

Calling this method removes all the occurrences of key, and returns the number of parameters removed.

Parameters:

key (Optional[str])

Return type:

int

static removePassword(aUri: str | None, hide: bool = False) str[source]

Removes the password element from a URI.

Parameters:
  • aUri (Optional[str]) – A data source uri

  • hide (bool = False) – True to replace the password value with ‘xxxxxxxx’, False to remove password (key and value) (since QGIS 3.34)

Return type:

str

Returns:

The data source uri without the password

schema(self) str[source]

Returns the schema stored in the URI.

Return type:

str

selectAtIdDisabled(self) bool[source]

Returns whether the selection by feature ID is disabled.

Return type:

bool

service(self) str[source]

Returns the service name associated with the URI.

See also

setService()

Return type:

str

setAuthConfigId(self, authcfg: str | None)[source]

Sets the authentication configuration ID for the URI.

Parameters:

authcfg (Optional[str])

setConnection(self, aHost: str | None, aPort: str | None, aDatabase: str | None, aUsername: str | None, aPassword: str | None, sslmode: QgsDataSourceUri.SslMode = QgsDataSourceUri.SslPrefer, authConfigId: str | None = '')[source]

Sets all connection related members at once.

Parameters:
  • aHost (Optional[str])

  • aPort (Optional[str])

  • aDatabase (Optional[str])

  • aUsername (Optional[str])

  • aPassword (Optional[str])

  • sslmode (QgsDataSourceUri.SslMode = QgsDataSourceUri.SslPrefer)

  • authConfigId (Optional[str] = '')

setConnection(self, aService: str | None, aDatabase: str | None, aUsername: str | None, aPassword: str | None, sslmode: QgsDataSourceUri.SslMode = QgsDataSourceUri.SslPrefer, authConfigId: str | None = '')[source]

Sets all connection related members at once (for a service case).

Parameters:
  • aService (Optional[str])

  • aDatabase (Optional[str])

  • aUsername (Optional[str])

  • aPassword (Optional[str])

  • sslmode (QgsDataSourceUri.SslMode = QgsDataSourceUri.SslPrefer)

  • authConfigId (Optional[str] = '')

setDataSource(self, aSchema: str | None, aTable: str | None, aGeometryColumn: str | None, aSql: str | None = '', aKeyColumn: str | None = '')[source]

Sets all data source related members at once.

The aSql argument represents a subset filter string to be applied to the source, and should take the form of a SQL “where” clause (e.g. “VALUE > 5”, “CAT IN (1,2,3)”).

Parameters:
  • aSchema (Optional[str])

  • aTable (Optional[str])

  • aGeometryColumn (Optional[str])

  • aSql (Optional[str] = '')

  • aKeyColumn (Optional[str] = '')

setDatabase(self, database: str | None)[source]

Sets the URI database name.

Parameters:

database (Optional[str])

setDriver(self, driver: str | None)[source]

Sets the driver name stored in the URI.

Parameters:

driver (Optional[str])

setEncodedUri(self, uri: str | None)[source]

Sets the complete encoded uri from a string value.

Parameters:

uri (Optional[str])

setGeometryColumn(self, geometryColumn: str | None)[source]

Sets geometry column name to geometryColumn

Added in version 3.10.

Parameters:

geometryColumn (Optional[str])

setHost(self, host: str | None)[source]

Sets the host name stored in the URI.

See also

host()

Added in version 3.42.

Parameters:

host (Optional[str])

setHttpHeaders(self, headers: QgsHttpHeaders)[source]

Sets headers to headers

Added in version 3.26.

Parameters:

headers (QgsHttpHeaders)

setKeyColumn(self, column: str | None)[source]

Sets the name of the (primary) key column.

Parameters:

column (Optional[str])

setParam(self, key: str | None, value: str | None)[source]

Sets a generic parameter value on the URI.

Note

If a parameter with the specified key already exists, another is inserted and the existing value is left unchanged.

Parameters:
  • key (Optional[str])

  • value (Optional[str])

setParamList(self, key: str | None, value: Iterable[str | None])

Sets a generic parameter list value on the URI.

Parameters:
  • key (Optional[str])

  • value (Iterable[Optional[str]])

setPassword(self, password: str | None)[source]

Sets the password for the URI.

Parameters:

password (Optional[str])

setPort(self, port: str | None)[source]

Sets the port stored in the URI.

See also

port()

Added in version 3.42.

Parameters:

port (Optional[str])

setSchema(self, schema: str | None)[source]

Sets the scheme for the URI.

Parameters:

schema (Optional[str])

setService(self, service: str | None)[source]

Sets the service name associated with the URI.

See also

service()

Added in version 3.42.

Parameters:

service (Optional[str])

setSql(self, sql: str | None)[source]

Sets the sql filter for the URI.

The sql represents a subset filter string to be applied to the source, and should take the form of a SQL “where” clause (e.g. “VALUE > 5”, “CAT IN (1,2,3)”).

See also

sql()

Parameters:

sql (Optional[str])

setSrid(self, srid: str | None)[source]

Sets the spatial reference ID associated with the URI.

Parameters:

srid (Optional[str])

setSslMode(self, mode: QgsDataSourceUri.SslMode)[source]

Sets the SSL mode associated with the URI.

See also

sslMode()

Added in version 3.42.

Parameters:

mode (QgsDataSourceUri.SslMode)

setTable(self, table: str | None)[source]

Sets table to table

Added in version 3.10.

Parameters:

table (Optional[str])

setUseEstimatedMetadata(self, flag: bool)[source]

Sets whether estimated metadata should be used for the connection.

Parameters:

flag (bool)

setUsername(self, username: str | None)[source]

Sets the username for the URI.

Parameters:

username (Optional[str])

setWkbType(self, type: Qgis.WkbType)[source]

Sets the WKB type associated with the URI.

Parameters:

type (Qgis.WkbType)

sql(self) str[source]

Returns the SQL filter stored in the URI, if set.

This represents a subset filter string to be applied to the source, and takes the form of a SQL “where” clause (e.g. “VALUE > 5”, “CAT IN (1,2,3)”).

See also

setSql()

Return type:

str

srid(self) str[source]

Returns the spatial reference ID associated with the URI.

Return type:

str

sslMode(self) QgsDataSourceUri.SslMode[source]

Returns the SSL mode associated with the URI.

See also

setSslMode()

Return type:

QgsDataSourceUri.SslMode

table(self) str[source]

Returns the table name stored in the URI.

Return type:

str

uri(self, expandAuthConfig: bool = True) str[source]

Returns the complete URI as a string.

Parameters:

expandAuthConfig (bool = True)

Return type:

str

useEstimatedMetadata(self) bool[source]

Returns True if estimated metadata should be used for the connection.

Return type:

bool

username(self) str[source]

Returns the username stored in the URI.

Return type:

str

wkbType(self) Qgis.WkbType[source]

Returns the WKB type associated with the URI.

Return type:

Qgis.WkbType