3

New to python, and been looking at this for a while, trying to write a scrip to automate a process we need to carry out in QGIS a few thousand times. As part of this I need to create a feature into an existing Postgres table without geometry. The existing table has approximately 40 different columsn, however I only need to populate the 6 as shown in the code.

I have the below code which I believe is correct, however the add features command doesnt seem to work, it returns False and doesnt write anything to the datbase.

def createNewIntermentRightRecord(layer,cemetery,yearsGranted,surname,firstName,otherName,liscadNo):
 if internRightsCaps & QgsVectorDataProvider.AddFeatures:
 feat = QgsFeature(layer.pendingFields())
 feat.initAttributes(6)
 feat.setAttributes(['Cemetery',cemetery])
 feat.setAttributes(['Years Granted',yearsGranted])
 feat.setAttributes(['IR Holder - LAST NAME',surname])
 feat.setAttributes(['IR Holder - First Name',firstName])
 feat.setAttributes(['IR Holder - Other Name/s'])
 feat.setAttributes(['Liscad Polygon No',liscadNo])
 (res,outFeats) = layer.dataProvider().addFeatures( [ feat ] )
 print res
 print outFeats
PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Mar 8, 2016 at 5:29

1 Answer 1

1

Around the method:

(res,outFeats) = layer.dataProvider().addFeatures( [ feat ] )

I think you miss a start edit and commit statement

def createNewIntermentRightRecord(layer,cemetery,yearsGranted,surname,firstName,otherName,liscadNo):
 if internRightsCaps & QgsVectorDataProvider.AddFeatures:
 feat = QgsFeature(layer.pendingFields())
 feat.initAttributes(6)
 feat.setAttributes(['Cemetery',cemetery])
 feat.setAttributes(['Years Granted',yearsGranted])
 feat.setAttributes(['IR Holder - LAST NAME',surname])
 feat.setAttributes(['IR Holder - First Name',firstName])
 feat.setAttributes(['IR Holder - Other Name/s'])
 feat.setAttributes(['Liscad Polygon No',liscadNo])
 layer.startEditing()
 (res,outFeats) = layer.dataProvider().addFeatures( [ feat ] )
 layer.commitChanges()
 print res
 print outFeats
answered Mar 8, 2016 at 8:27
3
  • Appreciate your help, definitely getting somewhere now, however still having issues. If I make the layer a shapefile, no problems writing to it.. However, if the layer in question is a postgres table which is in the layers panel it doesnt write to it, but doesnt throw any errors either. THe rest of the script reads from it fine though. Commented Mar 9, 2016 at 3:26
  • Do you have a primary key on the table in PostGIS? Check this for adding one: gis.stackexchange.com/questions/157541/… Commented Mar 9, 2016 at 7:00
  • Interesting you should suggest that I had a problem with that on a different table earlier this week. But yes I do have an primary key on that particular table. THis has led me to some more searching, I seem to ahve no problems adding to a new table in my postgres database. Have got a work around for now, but something to look into in the future. Your assistance is greatly appreciated! Commented Mar 10, 2016 at 3:26

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.