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?

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked May 17, 2020 at 0:46
3
  • Did you change geom_column = 'point' with geom_column = 'the_geo' ? Commented May 17, 2020 at 1:07
  • yes, but it doesn't work 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");
MrXsquared
36.2k22 gold badges76 silver badges127 bronze badges
answered Jun 21, 2021 at 8:58

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.