Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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

What am I currently doing?
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 ?

Thank you !

share|improve this question
    
Do you need it before or after you load the layer? –  Nathan W Sep 21 '12 at 10:10
1  
@NathanW It doesn't matter, it would be better before because it would mean that QGIS doesn't have to load every feature, but if it's impossible, never mind :) –  Julien Fouilhé Sep 21 '12 at 10:22
add comment

2 Answers 2

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

EDIT:

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 this answer for more details

share|improve this answer
    
Ah cool, it's possible ! Thanks a lot ! But I don't have that, I'm under QGIS 1.8.0-Lisboa (MAC OS). Do I have to install some plugin ? Did you click on "Add vector layer" to have this ? –  Julien Fouilhé Sep 21 '12 at 11:33
4  
There is no native MySQL provider for QGIS so you are not going to get a dialog like this. –  Nathan W Sep 21 '12 at 12:06
    
@NathanW Why not ? –  Julien Fouilhé Sep 21 '12 at 12:20
2  
Because I suspect that is the MSSQL/PostGIS open dialog. –  Nathan W Sep 21 '12 at 12:31
1  
I understand what you need. And this answer would be fine if your database was PostGIS/MS SQL. MySQL doesn't have this dialog because QGIS uses OGR to open the database. –  Nathan W Sep 21 '12 at 21:04
show 7 more comments
up vote 1 down vote accepted

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/

NB: 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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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