Programmatically creating a Spatialite database

36 views
Skip to first unread message

ckgoo...@gmail.com

unread,
Sep 14, 2025, 10:51:59 AM (11 days ago) Sep 14
to spatiali...@googlegroups.com

Hello,

I have C++ code to create a Spatialite database but it takes a long time to execute compared to creating a Spatialite database from the command prompt.

My code creates a sqlite3 database, enables extensions, loads mod_Spatialite and then runs:

rc = sqlite3_exec(m_sqlite3db, "SELECT InitSpatialMetadata();", nullptr, nullptr, &zErrMsg);

this can take around 30 seconds to run.  Doing it from the command line i.e. spatialite mydb.db, takes just a few seconds.

The file sizes are slightly different too between the two methods.  The programmatic one is a few hundred bytes smaller.

This isn’t really a big problem but would be interested to know if there is a way for the programmatic approach to be just as quick as the command line approach.

Best, Chris

sandro furieri

unread,
Sep 14, 2025, 11:20:13 AM (11 days ago) Sep 14
to spatiali...@googlegroups.com
Il 2025-09-14 16:22 ckgoo...@gmail.com ha scritto:
> Hello,
>
> I have C++ code to create a Spatialite database but it takes a long
> time to execute compared to creating a Spatialite database from the
> command prompt.
>
> rc = sqlite3_exec(m_sqlite3db, "SELECT InitSpatialMetadata();",
> nullptr, nullptr, &zErrMsg);
>

hello Chris,

you are calling the wrong function, and in bad way.

1) InitSpatialitMetadata() just creates the minimal context
for a Spatialite DB.
in order to create a full fledged v5.0 DB you need to
call instead InitSpatialiteMetedataFull()
(and this explains for the smaller DB size you noticed).

2. both functions accept an optional numeric argument; if
this is explicitly set to TRUE (1) all the meta-tables
will be created and populated as an automatically
managed SQL Transaction.
(and this explains for the longer time).

please see:
https://www.gaia-gis.it/gaia-sins/spatialite-sql-5.1.0.html#p16

SELECT InitSpatialMetaDataFull(1);

this is the correct way to create a v5.0 DB in a short time.

bye Sandro

ckgoo...@gmail.com

unread,
Sep 14, 2025, 5:02:15 PM (11 days ago) Sep 14
to spatiali...@googlegroups.com
Even, Sandro,
Thank you both. It now works much better. I didn't think of looking on that page for help.
Still learning.
Best, Chris
--
You received this message because you are subscribed to the Google Groups "SpatiaLite Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spatialite-use...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/spatialite-users/a2546e2e58a4f8d182c20bfb111bea96%40lqt.it.

Even Rouault

unread,
Sep 16, 2025, 2:17:52 AM (9 days ago) Sep 16
to spatiali...@googlegroups.com, ckgoo...@gmail.com

Hi,

cf https://www.gaia-gis.it/gaia-sins/spatialite-sql-5.1.0.html#p16 : "if the optional argument transaction is set to TRUE the whole operation will be handled as a single Transaction (faster)"

==> SELECT InitSpatialMetadata(TRUE)

--
You received this message because you are subscribed to the Google Groups "SpatiaLite Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spatialite-use...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/spatialite-users/004701dc2583%2411895a70%24349c0f50%24%40gmail.com.
-- 
http://www.spatialys.com
My software is free, but my time generally not.
Reply all
Reply to author
Forward
0 new messages