English 中文(简体)
计算QGIS的Length
原标题:SQLite / Geopackage AFTER INSERT trigger to calculate Length in QGIS

I have been following the guide here: https://www.northrivergeographic.com/adding-triggers-to-geopackage/ to add a trigger to a SQLite database in a Geopackage file. Geometry type is Line (LineStringZM).

我想这样做是自动计算线路长度,并在使用<代码”制作的新记录时更新一个叫Length的外地。 Toggle Editor > Add Line Substances in QGIS Digitizing Toolbar. 足够方便......但触发器似乎将更新记录all,并加上最新线的长度。

My code below:

CREATE TRIGGER update_Length_after_Insert AFTER INSERT
ON WaterwayOptions
BEGIN
UPDATE WaterwayOptions SET Length = (SELECT ST_Length(new.geom) FROM WaterwayOptions );
END

I thought about adding in a WHEN old.geom <> new.geom filter (as per example here: https://www.sqlitetutorial.net/sqlite-trigger/) clause but this will fail when creating a new record.

我逻辑上的错误是什么?

问题回答

我逻辑上的错误是什么?

在插入时,没有“old.? 只读new? 不用老一栏。 限定。

  • old. is only available for DELETEs and UPDATEs
  • new. is only available for INSERTs and UPDATEs

另外,您可能希望获得一个WHERE条款,而后一项WHEN条款则在触发触发启动时加以限制(这也要求获得关于普遍定期审议的任何途径的WHERE条款)。

UPDATE WaterwayOptions SET Length = (SELECT ST_Length(new.geom) FROM WaterwayOptions ) WHERE geom = new.geom;

如果你尝试使用WHEN条款,试图看看看新的<代码>geom是否以前不存在,那么你会有一个问题,因为“AFTER”在交易方面确实存在(EBEFORE受到警告)。

如果使用较为复杂的WHEN条款(见demo),发现新地段已经插入(见demo),那么UPDATE仍将更新所有各行。

因此,要求获得关于人民民主党的WHERE条款,因为关于人民民主党的WHERE条款很可能是最佳解决办法。

也许会考虑一下你似乎做些什么。

内容提要

  1. the original
  2. a second using the suggested WHERE clause,
  3. the third using a too simple WHEN clause, and finally
  4. the fourth using a more complex WHEN clause

除了建立TRIIGERTs之外,该法典是相同的(INSERTs the same data)。

The demo Code/SQL:-

/* ATTEMPT 1 (original)*/
DROP TABLE IF EXISTS WaterwayOptions;
CREATE TABLE IF NOT EXISTS WaterwayOptions (woId TEXT PRIMARY KEY, geom TEXT UNIQUE, `length` INTEGER, othercol TEXT DEFAULT  blah );

CREATE TRIGGER update_Length_after_Insert AFTER INSERT
ON WaterwayOptions
BEGIN
UPDATE WaterwayOptions SET Length = (SELECT Length(new.geom) FROM WaterwayOptions );
END
;

INSERT INTO WaterwayOptions (woid,geom,`length`)
    VALUES
    ( wo1 , geoma ,10),
    ( wo2 , geomaa ,10),
    ( wo3 , geomaaa ,10)
;
SELECT * FROM waterwayoptions;

/* ATTEMPT 2 (WHERE clause for the UPDATE)*/
DROP TABLE IF EXISTS WaterwayOptions;
CREATE TABLE IF NOT EXISTS WaterwayOptions (woId TEXT PRIMARY KEY, geom TEXT UNIQUE, `length` INTEGER, othercol TEXT DEFAULT  blah );
DROP TRIGGER IF EXISTS update_Length_after_Insert;
CREATE TRIGGER update_Length_after_Insert AFTER INSERT
ON WaterwayOptions
BEGIN
UPDATE WaterwayOptions SET Length = (SELECT Length(new.geom) FROM WaterwayOptions ) WHERE geom=new.geom;
END
;
INSERT INTO WaterwayOptions (woid,geom,`length`)
    VALUES
    ( wo1 , geoma ,10),
    ( wo2 , geomaa ,10),
    ( wo3 , geomaaa ,10)
;
SELECT * FROM waterwayoptions;

/* ATTEMPT 3 (simple WHEN) */
DROP TABLE IF EXISTS WaterwayOptions;
CREATE TABLE IF NOT EXISTS WaterwayOptions (woId TEXT PRIMARY KEY, geom TEXT UNIQUE, `length` INTEGER, othercol TEXT DEFAULT  blah );
DROP TRIGGER IF EXISTS update_Length_after_Insert;
CREATE TRIGGER update_Length_after_Insert AFTER INSERT
ON WaterwayOptions WHEN new.geom NOT IN (SELECT geom FROM WaterwayOptions)
BEGIN
UPDATE WaterwayOptions SET Length = (SELECT Length(new.geom) FROM WaterwayOptions );
END
;
INSERT INTO WaterwayOptions (woid,geom,`length`)
    VALUES
    ( wo1 , geoma ,10),
    ( wo2 , geomaa ,10),
    ( wo3 , geomaaa ,10)
;
SELECT * FROM waterwayoptions;


/* ATTEMPT 4 (more complex WHEN) */
DROP TABLE IF EXISTS WaterwayOptions;
CREATE TABLE IF NOT EXISTS WaterwayOptions (woId TEXT PRIMARY KEY, geom TEXT UNIQUE, `length` INTEGER, othercol TEXT DEFAULT  blah );
DROP TRIGGER IF EXISTS update_Length_after_Insert;
CREATE TRIGGER update_Length_after_Insert AFTER INSERT
ON WaterwayOptions WHEN new.geom NOT IN (SELECT geom FROM WaterwayOptions WHERE geom <> new.geom)
BEGIN
UPDATE WaterwayOptions SET Length = (SELECT Length(new.geom) FROM WaterwayOptions);
END
;
INSERT INTO WaterwayOptions (woid,geom,`length`)
    VALUES
    ( wo1 , geoma ,10),
    ( wo2 , geomaa ,10),
    ( wo3 , geomaaa ,10)
;
SELECT * FROM waterwayoptions;

成果(国家公务员系统之后的高等专业学校,因此,跨国激进党):

“enterography

  • First attempt ALL rows updated (incorrect)

“entergraph

  • UPDATE .... WHERE .... Each row has been updated to change 10 to the length of the geom column (correct)

enter image description here

  • nothing UPDATED??? (new geom row exists so IS in the table!!!!)

“在此处的影像描述”/</a

  • 因此,没有更新《世界人类发展报告》所有各行的WHEN条款,新地行被排除在外,不能在WHEN条款(BUT)中审议(以上第2段很可能是最佳解决办法)。

    • the demo uses the length function rather the the ST_length function as ST_length is not a standard built-in SQLite function.
      • hence the enclosures used around the length column to ensure that there is no ambiguity/conflict with the length function




相关问题
sqlite3 is chopping/cutting/truncating my text columns

I have values being cut off and would like to display the full values. Sqlite3 -column -header locations.dbs " select n.namelist, f.state, t.state from names n left join locations l on l.id = n.id ...

Entity Framework with File-Based Database

I am in the process of developing a desktop application that needs a database. The application is currently targeted to SQL Express 2005 and works wonderfully. However, I m not crazy about having ...

Improve INSERT-per-second performance of SQLite

Optimizing SQLite is tricky. Bulk-insert performance of a C application can vary from 85 inserts per second to over 96,000 inserts per second! Background: We are using SQLite as part of a desktop ...

Metadata for columns in SQLite v2.8 (PHP5)

How can I get metadata / constraints (primary key and "null allowed" in particular) for each column in a SQLite v2.8 table using PHP5 (like mysql_fetch_field for MySql)? sqlite_fetch_column_types (OO:...

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签