How to make a dynamic, calculated column? (Swift, SQLite)

224 Views Asked by At

How i can to make a dynamic, calculated column? Which is calculated using other columns and not stored in the database. Which can be used for data output and sorting.

Library: stephencelis/SQLite.swift (https://github.com/stephencelis/SQLite.swift)

Example:

SELECT a+b as c from test ORDER BY c ASC;

Thanks.

1

There are 1 best solutions below

1
On

With sqlite 3.31.0 and newer, you can use a generated column.

CREATE TABLE test(a INTEGER, b INTEGER,
    c INTEGER GENERATED ALWAYS AS (a + b) VIRTUAL);

Or use a view:

CREATE TABLE real_test(a INTEGER, b INTEGER);
CREATE VIEW test AS
    SELECT a, b, a + b AS c FROM real_test;