This is a sample C source showing how to use SQLite / SpatiaLite from C.This program shows the basic functionality that will be required for most SpatiaLite programs:
The typical output of this demo is shown below, when run against the sample database.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "config.h"
#include <sqlite3.h>
int
main (int argc, char *argv[])
{
int ret;
sqlite3 *handle;
sqlite3_stmt *stmt;
char sql[256];
int i;
int ic;
char **results;
int n_rows;
int n_columns;
char *err_msg = NULL;
int len;
char *table_name;
char **p_geotables = NULL;
int n_geotables = 0;
int row_no;
const void *blob;
int blob_size;
int geom_type;
double measure;
void *cache;
if (argc != 2)
{
fprintf (stderr, "usage: %s test_db_path\n", argv[0]);
return -1;
}
ret = sqlite3_open_v2 (argv[1], &handle, SQLITE_OPEN_READONLY, NULL);
if (ret != SQLITE_OK)
{
printf ("cannot open '%s': %s\n", argv[1], sqlite3_errmsg (handle));
sqlite3_close (handle);
return -1;
}
printf ("SQLite version: %s\n", sqlite3_libversion ());
printf ("\n\n");
strcpy (sql,
"SELECT DISTINCT f_table_name FROM geometry_columns ORDER BY 1");
ret = sqlite3_get_table (handle, sql, &results, &n_rows, &n_columns,
&err_msg);
if (ret != SQLITE_OK)
{
printf ("query#1 SQL error: %s\n", err_msg);
sqlite3_free (err_msg);
goto abort;
}
if (n_rows > 1)
{
n_geotables = n_rows;
p_geotables = malloc (sizeof (char *) * n_geotables);
for (i = 1; i <= n_rows; i++)
{
table_name = results[(i * n_columns) + 0];
len = strlen (table_name);
p_geotables[i - 1] = malloc (len + 1);
strcpy (p_geotables[i - 1], table_name);
}
sqlite3_free_table (results);
}
for (i = 0; i < n_geotables; i++)
{
printf ("========= table '%s' ========================\n",
p_geotables[i]);
sprintf (sql, "SELECT * FROM %s", p_geotables[i]);
ret = sqlite3_prepare_v2 (handle, sql, strlen (sql), &stmt, NULL);
if (ret != SQLITE_OK)
{
printf ("query#2 SQL error: %s\n", sqlite3_errmsg (handle));
goto abort;
}
n_columns = sqlite3_column_count (stmt);
row_no = 0;
while (1)
{
ret = sqlite3_step (stmt);
if (ret == SQLITE_DONE)
{
break;
}
if (ret == SQLITE_ROW)
{
row_no++;
printf ("row #%d\n", row_no);
for (ic = 0; ic < n_columns; ic++)
{
printf ("\t%-10s = ",
sqlite3_column_name (stmt, ic));
switch (sqlite3_column_type (stmt, ic))
{
case SQLITE_NULL:
printf ("NULL");
break;
case SQLITE_INTEGER:
printf ("%d", sqlite3_column_int (stmt, ic));
break;
case SQLITE_FLOAT:
printf ("%1.4f",
sqlite3_column_double (stmt, ic));
break;
case SQLITE_TEXT:
printf ("'%s'",
sqlite3_column_text (stmt, ic));
break;
case SQLITE_BLOB:
blob = sqlite3_column_blob (stmt, ic);
blob_size = sqlite3_column_bytes (stmt, ic);
geom =
blob_size);
if (!geom)
{
printf ("BLOB [%d bytes]", blob_size);
}
else
{
printf ("EMPTY or NULL GEOMETRY");
else
{
char *geom_name;
geom_name = "POINT";
geom_name = "LINESTRING";
geom_name = "POLYGON";
geom_name = "MULTIPOINT";
if (geom_type ==
geom_name = "MULTILINESTRING";
if (geom_type ==
geom_name = "MULTIPOLYGON";
if (geom_type ==
geom_name =
"GEOMETRYCOLLECTION";
printf ("%s SRID=%d", geom_name,
|| geom_type ==
{
#ifndef OMIT_GEOS
&measure);
printf (" length=%1.2f",
measure);
#else
printf
(" length=?? [no GEOS support available]");
#endif
}
geom_type ==
{
#ifndef OMIT_GEOS
&measure);
printf (" area=%1.2f",
measure);
#else
printf
("area=?? [no GEOS support available]");
#endif
}
}
}
break;
};
printf ("\n");
}
if (row_no >= 5)
{
break;
}
}
else
{
printf ("sqlite3_step() error: %s\n",
sqlite3_errmsg (handle));
sqlite3_finalize (stmt);
goto abort;
}
}
sqlite3_finalize (stmt);
printf ("\n\n");
}
ret = sqlite3_close (handle);
if (ret != SQLITE_OK)
{
printf ("close() error: %s\n", sqlite3_errmsg (handle));
return -1;
}
printf ("\n\nsample successfully terminated\n");
for (i = 0; i < n_geotables; i++)
{
free (p_geotables[i]);
}
free (p_geotables);
return 0;
abort:
sqlite3_close (handle);
if (p_geotables)
{
for (i = 0; i < n_geotables; i++)
{
free (p_geotables[i]);
}
free (p_geotables);
}
return -1;
}