0

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?

3
  • Did you change geom_column = 'point' with geom_column = 'the_geo' ?
    – ThomasG77
    Commented May 17, 2020 at 1:07
  • yes, but it doesn't work
    – Sommo
    Commented May 17, 2020 at 20:32
  • Is the path to the file actually correct? Commented Jul 26, 2021 at 17:29

1 Answer 1

-2
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");

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.