<div dir="ltr"><div><div>Hi Spencer, <br></div>I'm moving the thread to qgis-dev ML.<br><br></div><div>Instead of using the postgis_utils.py module, <br></div><div>just import db_manager/db_plugins/postgis/connector.py <br>and create an instance of PostGisDBConnector.</div><div>That class allows you to do everything you need, even run a sql query.<br><br></div><div>Here's a code snippet acting on the active PostGIS layer:<br><br><div>from db_manager.db_plugins.postgis.connector import PostGisDBConnector<br><br></div><div>vl = iface.activeLayer()<br></div><div>uri = QgsDataSourceURI( vl.source() )<br></div><div>conn = PostGisDBConnector( uri )<br><br>sql = u"SELECT * FROM mytable"<br></div><div>cursor = conn._execute( None, sql )<br></div><div>for row in conn._fetchall( cursor ):<br></div><div> print row<br></div><div>conn._close_cursor( cursor )<br></div><br></div><div>sql = u"UPDATE mytable SET col1='5' "<br></div><div>conn._execute_and_commit( sql )<br><br><br></div><div>For more usage examples, have a look at connector.py code, <br></div><div>or just ask ;-)<br><br></div><div>Cheers!<br></div><div>Giuseppe<br></div><div dir="ltr"><div><div><div><br><div class="gmail_quote"><div dir="ltr">On Thu, Oct 22, 2015 at 2:09 PM Spencer Gardner <<a href="mailto:spencergardner@gmail.com" target="_blank">spencergardner@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>I have created a QGIS plugin that adds several tools to the QGIS Processing framework. Many of the tools need to communicate with a PostGIS database. Up to now, I have accomplished this using a helper utility included in the Processing framework. (See <a href="https://lists.osgeo.org/pipermail/qgis-developer/2015-October/039609.html" target="_blank">this</a> mailing list discussion for more details.)</div><div><br></div><div>I'm now investigating the possibility to switch to using the db_manager plugin for all of the database work. I'm testing on my own tools right now, although this could eventually replace the utility that is included within Processing for all algorithms.</div><div><br></div><div>I'm having some trouble figuring out how to execute a simple SQL query on my database and I'm hoping that you can provide an example for me to refer to. I can successfully create a layer from a sql query using the toSqlLayer() function, but I don't see an easy way to just run a query.</div><div><br></div><div>For example, some of my tools involve running an UPDATE operation on a table. How could I accomplish that?</div></div></blockquote><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><br></div><div>Thanks for your help,</div><div>Spencer</div></div>
</blockquote></div></div></div></div></div></div><div dir="ltr">-- <br></div><div dir="ltr">Giuseppe Sucameli<br></div>