I'm new to SpatiaLite and PyQGIS.
I would like to upload a layer from SpatiaLite database. To do this, I tried with this code:
database = r"C:\sqlite\db.sqlite"
uri = QgsDataSourceUri()
uri.setDatabase(database)
schema = ''
table = 'table_name'
geom_column = 'point'
uri.setDataSource(schema, table, geom_column)
display_name = 'table_name'
vlayer = QgsVectorLayer(uri.uri(), display_name, 'spatialite')
QgsProject.instance().addMapLayer(vlayer)
But this doesn't work, the program says:
Unavailable layer! Layer datasource could not be found
I think the problem may be the value of "geom_column". The table has a column called "the_geo", where the row values are POINT(x_value, y_value).
Can you help me?
1 Answer 1
int result;
QString sql = QStringLiteral("select table_name, type, label_field from kt_master");
sqlite3_statement_unique_ptr preparedStatement = mDatabase.prepare(sql, result);
QMap<QString, QList<QString>> masterInfo;
masterInfo.clear();
while (preparedStatement.step() == SQLITE_ROW)
{
QgsDataSourceUri uri = QgsDataSourceUri();
uri.setDatabase(mFilePath);
uri.setDataSource("", preparedStatement.columnAsText(0), "geom");
MrXsquared
36.2k22 gold badges76 silver badges127 bronze badges
default
geom_column = 'point'
withgeom_column = 'the_geo'
?