2

I would like to get only one feature from a MySQL database.

I use the Add Vector Layer functionality in QGIS to get the features (polygons) from the MySQL database. They are all displaying fine, but my problem is that I would like to get only one feature/polygon from the database, filtering by a MySQL query by example (it would be perfect).

Is there a way to do so ?

0

2 Answers 2

3

When you connect to your database and after you selected the table your wish to view, there is a button which says "Build Query". And then you tell qgis which features you want to fetch.

connect-window query-window

If for some reason you cannot access the build-in query builder you can create a View.

Go in the database and enter,

CREATE OR REPLACE VIEW myView AS SELECT * FROM <SPATIAL_TABLE> where <constraints>;

And then point QGIS at the created view. Also check How to create a table from existing tables (applying new schemas) in postgis DB? for more details.

0
2

I found a solution using the ogr2ogr executable, which supports MySQL (if you're using PostgreSQL, see pgsql2shp and shp2pgsql). I'm using it like this and it works fine.

ogr2ogr -overwrite -where "YOUR SQL CONDITION" -f "ESRI Shapefile" SHPCREATED.shp MYSQL:vine,host=localhost,user=root YOURTABLE

If you're under Mac OS X, the executable has been installed in /Library/Frameworks/GDAL.framework/Versions/1.9/Programs/

I was looking for a solution which was possible in command-line, because I'm working on a website and I would like to do these tasks automatically. Therefore, this is perfect for me.

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.