3

I want to add a feature class of a personal geodatabase mdb to my qgs project by code (a plugin I am working on).

I am using a command similar to that used for adding shapefiles, but this is not working. I guess there must be a way, since it is possible to add the layer using the Add Vector Layer button.

This is the code i am using on the Python console:

mylayer = QgsVectorLayer("D:\Test\test.mdb\points", "mypoints", "ogr")
QgsMapLayerRegistry.instance().addMapLayer(mylayer)

The first line runs without error but the second one does not produce any result, contrary to the result is I do a similar command using a shapefile.

I am using QGIS 2.2 Valmiera 32 bits on Windows 7 64 bits. I did a previous research but I did not find any results!!! The feature class is not inside a feature dataset.


I guess I would have to use a QgsDataSourceURI(), but i can not figure out the syntax. I have been trying. Also, I would also want to add that migrating the mdb is not an option, I have requested this before but my superiors have refused. And this geodatabase (points) is updated daily and huge. My goal after loading is clipping it based on a zone of interest.

1 Answer 1

2

Well is was easier than expected, not ODBC stuff, just the following code:

mylayer = QgsVectorLayer(r"D:\Test\test.mdb|layername=points", "mypoints", "ogr")
QgsMapLayerRegistry.instance().addMapLayer(mylayer)

I hope it will work on the newer versions of QGis.

1
  • QgsMapLayerRegistry doesn't work on v3.x, probably just replace with QgsProject.instance().addMayLayer()
    – nr_aus
    Nov 30, 2021 at 7:16

Your Answer

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

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