I have a table loaded in QGIS from spatialite DB. When i see the layer properties it is dbname='/home/farook/Spatialite/Cities.sqlite' table="cities" sql=. Now i want the dbname alone (i.e) the entire path '/home/farook/Spatialite/Cities.sqlite' for whatever layer loaded inside QGIS dynamically using PyQGIS. Is there any options for the same?

up vote 3 down vote accepted

These two code statements will provide the table path to the db name:

print iface.activeLayer().dataProvider().dataSourceUri().split('|')[0]

or

print lyr.dataProvider().dataSourceUri().split('|')[0]
  • I tried but its prints dbname='/home/farook/Spatialite/Cities.sqlite' table="cities" (geometry) sql= instead of dbname='/home/farook/Spatialite/Cities.sqlite' – Farook Apr 13 '16 at 5:20
  • For your case you may have to split at the empty space character between Cities.sqlite' and table instead of the pipe character e.g. .split(' ')[0] – artwork21 Apr 13 '16 at 11:30
  • Aww. Yeah. Thanks. – Farook Apr 13 '16 at 11:34

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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