[GIS] How to add an attribute with QGIS 2.8.1 Python console

fields-attributespyqgis

i would like to add an attribute with the python console:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
layer = iface.addVectorLayer("C:/donnees_SIG/05.shp","nou","ogr")
layer = iface.activeLayer()
layer.startEditing()
layer.addAttribute(QgsField("STATUT", QVariant.Int)) 

But it doesn't work…
When i write the last line, the python console returns 'False'.

I'm using QGIS 2.8.1

Best Answer

Look at Using Vector Layers: Adding and Removing Fields

res = layer.dataProvider().addAttributes([QgsField("STATUT", QVariant.Int)])
layer.updateFields()