1

I'm creating a Qgis python plugin that allows inserting shp into existing Postgis table, i've tried using ogr2ogr and it's workin when I run the command in FWTools Shell but when I try to run it in python console in QGIS it doesn't work.
the command that I executed in FWTools Shell:

ogr2ogr -update -append -f PostgreSQL PG:"dbname=geodb user=postgres password=admin" myshp.shp -nln existing_table

the code in python console:

import os
import process
cmd='ogr2ogr -update -append -f PostgreSQL PG:"dbname=geodb host=localhost port=5432 user=postgres password=admin" myshp.shp -nln existing_table'
input_command = [cmd]
program='C:/Program Files (x86)/FWTools2.4.7/bin/ogr2ogr.exe'
command_run = subprocess.call([program, input_command])
if command_run == 0:
    print "Its worked!!"
else:
    print 'no'

EDIT:
I've changed the code and it's workin now !
I've deleted the program name from cmd and added fieldmap for mapping

import os
import process
cmd='-update -append -fieldmap 0,1,2,3,4,5,6,7,8,10,9,11 -f PostgreSQL PG:"dbname=geodb host=localhost port=5432 user=postgres password=admin" myshp.shp -nln existing_table'
input_command = [cmd]
program='ogr2ogr.exe'
command_run = subprocess.call([program, input_command])
if command_run == 0:
    print "Its worked!!"
else:
    print 'no'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.