<div dir="ltr"><div>Leaving source code for future reference:<br></div><div><br></div><div>Exporting Shapefile or Vector Layer to Spatialite using QgsVectorFileWriter<br></div><div><br></div><div><div><br></div><div><div># -*- coding: utf-8 -*-</div>
<div>import sys</div><div><br></div><div>from qgis.core import *</div><div><br></div><div>app = QgsApplication(sys.argv, False)</div><div><br></div><div># need to locate srs.db</div><div>app.setPkgDataPath( '/usr/local/share/qgis' )</div>
<div><br></div><div># need to locate qgis libraries</div><div>app.setPrefixPath( '/usr/local' )</div><div><br></div><div># need to locate c++ plugins (*.so)</div><div>app.setPluginPath( '/usr/local/lib/qgis/plugins' )</div>
<div><br></div><div>app.initQgis()</div><div># show the environment variables</div><div>print 'lib path:', app.libraryPath()</div><div>print 'plugin path:', app.pluginPath()</div><div>print 'srs.db:', app.srsDbFilePath()</div>
<div>#</div><div>vlayer = QgsVectorLayer('track.shp', 'layer_name_you_like', 'ogr')</div><div>#</div><div>crs = vlayer.crs()</div><div>#</div><div>if not crs.isValid():</div><div>    # choose a 4326 - WGS84 CRS</div>
<div>    crs = QgsCoordinateReferenceSystem( 4326, QgsCoordinateReferenceSystem.EpsgCrsId )</div><div><br></div><div>error = QgsVectorFileWriter.writeAsVectorFormat( vlayer,</div><div>                                                 'test.sqlite',</div>
<div>                                                 'System',</div><div>                                                 crs,</div><div>                                                 'SQLite',</div><div>
                                                 False,</div><div>                                                 None,</div><div>                                                 ["SPATIALITE=YES",] )</div><div>
if error != QgsVectorFileWriter.NoError:</div><div>    print 'Error number:', error</div></div></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Jul 15, 2013 at 2:19 PM, Daniel <span dir="ltr"><<a href="mailto:danielvaz@gmail.com" target="_blank">danielvaz@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_extra">Answer:</div><div class="gmail_extra"><br></div><div class="gmail_extra">
vlayer = QgsVectorLayer('track.shp', 'layer_name_you_like', 'ogr')</div>
<div class="gmail_extra"><br></div><div class="gmail_extra">error_msg = ''</div><div class="gmail_extra"><br></div><div class="gmail_extra">error = QgsVectorFileWriter.writeAsVectorFormat( vlayer, 'test.sqlite', 'System', vlayer.crs(), 'SQLite', False, error_msg, [ "SPATIALITE=YES" , ] )</div>

<div class="gmail_extra"><br></div><div class="gmail_extra">where:</div><div class="gmail_extra"><br></div><div class="gmail_extra">vlayer                             =>  vector layer which will be exported to spatialite</div>

<div class="gmail_extra">'test.sqlite'                      =>  absolute path to new spatialite database file</div><div class="gmail_extra">'System'                         =>  string that represents the file encoding</div>

<div class="gmail_extra">vlayer.crs()                      =>  vector layer crs</div><div class="gmail_extra">'SQLite'                           =>  provider name</div><div class="gmail_extra">False                              =>  export onlySelected?</div>

<div class="gmail_extra">error_msg                        =>  if there is any error message will be put in that variable (if user don't care about error handling, set to None)</div><div class="gmail_extra">[ "SPATIALITE=YES" , ]   =>  database options. (here is the trick)</div>

<div class="gmail_extra"><br></div><div class="gmail_extra"><br></div><div class="gmail_extra"><br></div><div class="gmail_extra">Also, there is a class <a href="http://www.qgis.org/api/classQgsVectorLayerImport.html" target="_blank">http://www.qgis.org/api/classQgsVectorLayerImport.html</a><br>

</div><div class="gmail_extra"><br></div><div class="gmail_extra">But, I didn't have success in create an empty database using the QgsVectorLayerImport. I think that class (QgsVectorLayerImport) expects a created spatialite database.</div>
<span class="HOEnZb"><font color="#888888">
</font></span></div><span class="HOEnZb"><font color="#888888"><div class="gmail_extra"><div><br></div>-- <br>Daniel Vaz
</div></font></span></div>
</blockquote></div><br><br clear="all"><div><br></div>-- <br>Daniel Vaz
</div>