gvSIG + Python = accesing MySQL

Facebooktwitterredditpinterestlinkedinmail

La versión 2.3 de gvSIG Desktop no permite añadir capas a partir de una base de datos MySQL. Esto se puede resolver de forma sencilla usando la estupenda capacidad de scripting Python de gvSIG; basta con incluir el driver JDBC apropiado en el directorio de gvSIG, conectarse a la base de datos, y hacer la consulta deseada. A partir de ahí, pueden crearse las capas necesarias y añadirlas a nuestro proyecto, como se muestra en esta otra entrada del blog. Al final de la entrada (y también en mi repositorio GitHub) puede verse el código mínimo para acceder a la base de datos y hacer una consulta.

gvSIG Desktop 2.3 does not allow to add layers from a MySQL database. This can be easily solved using the great Python scripting utilities provided by gvSIG; you just have to include the proper JDBC driver in your gvSIG folder, then connect to the database and do your query. Those data can be stored in a new layer and then added into our proyect, as it is shown in this previous post. At the end of this post (and also in my GitHub repository) you can see the minimun code required to accesing a database and retrieving information.

 

import sys
import os
from gvsig import *
from gvsig import geom
from gvsig.geom import *
from java.util import Properties

sys.path.append(“/pathtomygvSIGfolder/mysql-connector-java-5.1.40-bin.jar”);
import org.gjt.mm.mysql.Driver as Driver

def main():

# Connect to the database
props = Properties()
props.put(“user”,”myuser”)
props.put(“password”,”mypasswd”)
db = Driver().connect(“jdbc:mysql://localhost/mydb”, props)
# Get geometry info from database and insert it into the new layer
c = db.createStatement()
rs = c.executeQuery(“select * from mytable where somecolumn=’somevalue'”)
while rs.next():
id = rs.getString(“somecolumn”)
rs.close()
c.close()
db.close()