The goal of this recipe is identical to the previous recipe, but it covers how to perform the process with data in a SpatiaLite database. You will to turn points into lines and lines into polygons.
Not all methods are available; for those that are not available, you can use the previous recipe. It will also work on a database layer; it just doesn't save the results to the database. So, the results will need to be imported to the database after completion.
You need to load a vector layer of points with a numeric ID indicating order, and an identifier of unique lines or polygons that is shared between points of the same geometry. For example, you can use census_wake_2000_points loaded into SpatiaLite with the geometry field called geom.
Using DB Manager Plugin (comes with QGIS and is in the Database menu), the QspatiaLite plugin, or an alternate SpatiaLite SQL application (command line or GUI), the following SQL examples will perform the conversions between vector types.
--Create table grouping points with shared stfid into lines CREATE Table census_pts2lines AS SELECT stfid,MakeLine(geom) as geom FROM census_wake_2000_points GROUP BY stfid;
The following screenshot shows what the screen will look like:

--Register the new table's geometry so QGIS knows its a spatial layer
SELECT RecoverGeometryColumn('census_pts2lines','geom',3358,'LINESTRING',2);Some SQL interfaces can run multiple SQL statements in a row, separated by a semicolon. However, there are also many interfaces that can only perform one query at a time. Generally, run one query at a time unless you know your software supports multiple queries; otherwise, this may fail or silently only run the first query.
--Create table grouping lines with shared stfid into polygons CREATE Table census_line2poly AS SELECT stfid,ST_Polygonize(geom) as geom FROM census_pts2lines GROUP BY stfid;
--Register the new table's geometry so QGIS knows its a spatial layer
SELECT RecoverGeometryColumn('census_line2poly','geom',3358,'POLYGON',2);Based on the common identifier specified in GROUP BY, the SQL statement aggregates multiple points into a new geometry of the type specified. After creating the new geometry and saving the results to a table, registration of the spatial metadata allows Spatialite and QGIS to know the table is a spatial layer.
In the second example, lines were converted to polygons. You could also go directly from points to polygons with ST_Polygonize(ST_MakeLine(geom)).
Under the current versions of SpatiaLite, only aggregation to higher levels is fully supported. If you wish to disaggregate geometries, you can use the QGIS vector tools from the previous recipe.