1

I am a novice to Python and QGIS coding. I am facing an issue while showing the MySQL spatial data on QGIS canvas. Database connection details are below:

database name:- mysql
host :- localhost
port :- 3306
user :- root
password :- abcx
tablename:- Tablegis

I found a code on to connect the same:

uri="MySQL:mysql,host=localhost,port=3306,user=root,password=abcx|layername=Tablegis"
vlayer=QGSVectorLayer(uri, "Open_Table","ogr")
QgsMapLayerRegistry.instance().addMapLayer(vlayer)

This code doesn't show any warning or error but nothing shows on my canvas. When I am printing the vlayer shows the object reference. Tried hard nothing worked for me.

  • does the layer display if you add it manually using the menus – Ian Turton Jun 12 '19 at 9:12
  • Nothing on the canvas but can you access the content of the attribute table ? Are you sure of the geographical content of your table ? – snaileater Jun 12 '19 at 14:03
  • Yes Ian, when I am manually adding the MySQL table in QGIS it displays and can also see the attributes. – Manoj Jun 13 '19 at 5:33
  • code is okay, i am just missing the update extent and zoom the canvas to objects of table. – Manoj Dec 31 '19 at 9:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.