简体   繁体   English

如何将字段添加到qgis中加载的postgis层

[英]How to add fields to postgis layer loaded in qgis

I have loaded the layer from Postgis in qgis and i am adding the fields to the layer using the follo code snippet: 我已经从qgis中的Postgis加载了该层,并使用以下代码段将字段添加到了该层:

canvas = qgis.utils.iface.mapCanvas()
aLayer = canvas.currentLayer()
field_name = "test"

aLayer.startEditing()
if not field_name in fieldnames:
      res = aLayer.dataProvider().addAttributes([QgsField(field_name, QVariant.String)])
aLayer.updateFields()
aLayer.commitChanges()

but it is not adding the field to the layer. 但它不会将字段添加到图层中。 What i have to do if i want to add the fields to the layer. 如果我想将字段添加到图层,该怎么办。

you forgot to add the type name. 您忘记添加类型名称。 just use QgsField(field_name, QVariant.String, 'varchar') 只需使用QgsField(field_name, QVariant.String, 'varchar')

you might want to add length definition too. 您可能也想添加长度定义。

you can have a look at the sip file 你可以看一下SIP文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Python 控制台将 PostGIS SQL SELECT 查询作为图层添加到 QGIS 3 项目? - How to add PostGIS SQL SELECT query as layer to QGIS 3 project using Python console? PostGIS和QGIS的连接问题? - PostGIS and connection to QGIS issue? 如何通过创建唯一ID来添加新字段并将其附加到postGis中的旧DB中? - How to add new fields by creating unique ID and append it in older DB in postGis? 分组唯一行值Postgis,postgres和Qgis - Grouping Unique Row values Postgis, postgres and Qgis 如何在PHP和postgresql / postgis代码中使用JavaScript Geojson对象作为图层 - How to use JavaScript Geojson object in PHP and postgresql/postgis code as a layer 将时间戳字段添加到ogr2ogr导入Postgis - Add timestamp fields to ogr2ogr import into Postgis 如何将 PostGIS 添加到 PostgreSQL pgAdmin? - How do I add PostGIS to PostgreSQL pgAdmin? 如何将栅格数据加载到运行postgis的postgres服务器中? 使用sql函数或QGIS有哪些选择? - How can I load raster data into a postgres server running postgis? What are my options using sql functions or QGIS? 通过 SSL 连接到 Crunchy HA PostgreSQL Postgis 数据库的 QGIS 错误 - QGIS error connecting to Crunchy HA PostgreSQL Postgis database via SSL 如何在 QGIS 的 Postgresql 数据库层中将几何类型从 LINESTRING 更改为 MULTILINESTRING - How to change geometry type from LINESTRING to MULTILINESTRING in a Postgresql database layer in QGIS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM