May 19, 2015
Index Link Error When Building Application with SQLite3 Library ....................................................................... 3
2 forms of LIMIT clause....................................................................................................................................... 4
About sqlite3_get_table()...................................................................................................................................... 5
C Sample Codes..................................................................................................................................................... 7
SQLite Manager (Firefox Add-Ons) ..................................................................................................................11
Link Error When Building Application with SQLite3 Library
When compiling an application which uses SQLite3 library, it might happen “Link Error” that compiler could
not find PThread (POSIX Thread) symbol’s definition as below snapshot.
<Solution>
In the Make file of that application, add Pthread link parameter in GCC built-in Make variable for linker ,
“LDFLAGS”, as below.
$(CC) -o APP $(SRC) $(LDFLAGS)
Or add it in “gcc” command parameter list :
gcc -o APP APP-SRCs -lsqlite3 -lpthread -ISQLite_SRC/.libs
2 forms of LIMIT clause
LIMIT clause is used to limit the data amount returned by the SELECT statement.
There are two equivalent forms of LIMIT :
[Type-1]
LIMIT <skip>, <count>
[Type-2]
LIMIT <count> OFFSET <skip>
Example:
Given 6 records in table “employee” as below.
employee
ID NAME
1 Alice
2 Bob
3 Clyde
4 Derek
5 Eric
6 Fred
If it wants to select 3 records from 2nd position, the statement could be:
SELECT * FROM employee LIMIT 1, 3
or
SELECT * FROM employee LIMIT 3 OFFSET 1
, and its result is :
ID NAME
2 Bob
3 Clyde
4 Derek
.
About sqlite3_get_table()
int sqlite3_get_table(
sqlite3 *db, /* An open database */
const char *zSql, /* SQL to be evaluated */
char ***pazResult, /* Results of the query */
int *pnRow, /* Number of result rows */
int *pnColumn, /* Number of result columns */
char **pzErrmsg /* Error msg written here */
);
The table conceptually has a number of rows and columns. But these numbers are not part of the result table
itself. These numbers are obtained separately.
A result table is an array of pointers to zero-terminated UTF-8 strings.
As an example of the result table format, suppose a query result is as follows:
ID NAME
2 Bob
3 Clyde
4 Derek
.
The content of “pazResult” parameter is as below :
pazResult [0] = "ID";
pazResult [1] = "NAME";
pazResult [2] = "2";
pazResult [3] = "Bob";
pazResult [4] = "3";
pazResult [5] = "Clyde";
pazResult [6] = "4";
pazResult [7] = "Derek";
.
If you want to use 1st field value of 1st record, use this
pazResult[pnColumn]
where “pnColumn” is the number column and this means skipping first “pnColumn” elements of array of result
table and is retrieved through sqlite3_get_table() function. For 2nd field value of 1st record is
pazResult[pnColumn + 1]
Table Headers
1st record
2nd record
3rd record
For 1st field value of 2nd record is
pazResult[pnColumn * 2]
and 2nd field value of 2nd record is
pazResult[pnColumn * 2 + 1]
.
Retrieving other records uses the same rule.
C Sample Codes
<Get Total Number of Records of a Table>
#include <stdlib.h>
#include "sqlite3.h"
/* Macro */
#define SQLite_DBNAME “PathTo/A_SQLite_DB.db”
int GetTotalNumOfRec()
{
/* Variables declaration */
sqlite3 *db = NULL; /* An open database */
char *zErrMsg = NULL; /* Error msg written here */
char **dbResult = NULL; /* Results of the query */
char sqlStmt[512] = {0}; /* Store SQL statement */
int retSql; /* Result of execution of SQL statement. */
int nRow = 0; /* Number of result rows */
int nColumn = 0; /* Number of result columns */
char result[100] = {0}; /* Store the responding message. */
int nRC = 0; /* Record Count in integer type. */
/* Try to connect a SQLite3 database, e,g, a SQLite3 .db file. */
if (sqlite3_open (SQLite_DBNAME, &db) == SQLITE_OK)
{
sprintf(sqlStmt, "SELECT count(*) AS cnt FROM employee;");
retSql = sqlite3_get_table (db, sqlStmt, &dbResult, &nRow, &nColumn, &zErrMsg);
if (retSql != SQLITE_OK)
{
fprintf (stderr, "%s: %s(%d) - SQLite Err [ %s ] \n",
__FILE__, __FUNCTION__, __LINE__, zErrMsg);
sqlite3_free (zErrMsg); /* Release memory */
}
else
{
sprintf(result, "%s", dbResult[1]);
nRC = atoi (result); /* convert to integer */
sqlite3_free_table (dbResult); /* Release memory */
sqlite3_close (db); /* Release memory */
return nRC; /* Return amount of records */
}
sqlite3_close (db); /* Release memory */
return 0;
}
else
fprintf(stderr, "%s: %s(%d) - Can Not open SQLite3 database [ %s ] \n",
__FILE__, __FUNCTION__, __LINE__, SQLite_DBNAME);
sqlite3_close (db); /* Whether the database is connected successfully or not, it needs to close the
database object for releasing allocated memory. */
return 0;
}
<Iterate Retrieved Records – Type 1>
#include "sqlite3.h"
/* Macro */
#define SQLite_DBNAME “PathTo/A_SQLite_DB.db”
/* Variables declaration */
sqlite3 *db = NULL; /* An open database */
char *zErrMsg = NULL; /* Error msg written here */
char **dbResult = NULL; /* Results of the query */
char sqlStmt[512] = {0}; /* Store SQL statement */
int retSql; /* Result of execution of SQL statement. */
int nRow; /* Number of result rows s */
int nColumn; /* Number of result columns */
/* Try to connect a SQLite3 database, e,g, a SQLite3 .db file. */
if (sqlite3_open(SQLite_DBNAME, &db) == SQLITE_OK)
{
/* select fields “ID” and “NAME” from table “employee” */
sprintf(sqlStmt, "SELECT ID, NAME FROM employee ';");
retSql = sqlite3_get_table (db, sqlStmt, &dbResult, &nRow, &nColumn, &zErrMsg);
if (retSql != SQLITE_OK)
{
fprintf (stderr, "%s: %s(%d) - SQLite Err [ %s ] \n", __FILE__, __FUNCTION__, __LINE__, zErrMsg);
sqlite3_free (zErrMsg); /* Release memory */
}
else if( (retSql == SQLITE_OK) && (nRow > 0) )
{
int i;
printf ("%s: %s(%d) - Row [ %d ] \n", __FILE__, __FUNCTION__, __LINE__, nRow);
/* Go through all retrieved records */
for(i = 1; i <= nRow; i++)
{
int j;
/* Go through all retrieved records */
for(j = 1; j <= nRow; j++)
printf("%s: %s(%d) – #%d : ID[ %s ] , Name[ %s ] \n",
__FILE__, __FUNCTION__, __LINE__,
j, dbResult[nColumn * j], dbResult[nColumn * j + 1]);
} /* end of for( nRow ) */
sqlite3_free_table (dbResult);
} /* end of if( nRow > 0 ) */
else /* nRow == 0 */
sqlite3_free_table (dbResult);
} /* end of if database is connected successfully */
else
fprintf(stderr, "%s: %s(%d) - Can Not open SQLite3 database [ %s ] \n",
__FILE__, __FUNCTION__, __LINE__, SQLite_DBNAME);
sqlite3_close (db); /* Whether the database is connected successfully or not, it needs to close the
database object for releasing allocated memory. */
<Iterate Retrieved Records – Type 2 : Using LIMIT Clause >
#include "sqlite3.h"
/* Macro */
#define SQLite_DBNAME “PathTo/A_SQLite_DB.db”
/* Variables declaration */
sqlite3 *db = NULL; /* An open database */
char *zErrMsg = NULL; /* Error msg written here */
char **dbResult = NULL; /* Results of the query */
char sqlStmt[512] = {0}; /* Store SQL statement */
int retSql; /* Result of execution of SQL statement. */
int nRow; /* Number of result rows s */
int nColumn; /* Number of result columns */
/* Try to connect a SQLite3 database, e,g, a SQLite3 .db file. */
if (sqlite3_open(SQLite_DBNAME, &db) == SQLITE_OK)
{
int recCnt = GetTotalNumOfRec(); /* Record Count */
int recNo = 0;
/* Iterate all records */
for(recNo = 0; recNo < recCnt; recNo++)
{
/* select “recNo”nd record from table “employee” */
sprintf(sqlStmt, "SELECT ID, NAME FROM employee LIMIT %d,1;", recNo);
retSql = sqlite3_get_table (db, sqlStmt, &dbResult, &nRow, &nColumn, &zErrMsg);
if (retSql != SQLITE_OK)
{
fprintf (stderr, "%s: %s(%d) - SQLite Err [ %s ] \n",
__FILE__, __FUNCTION__, __LINE__, zErrMsg);
sqlite3_free (zErrMsg); /* Release memory */
}
else if( (retSql == SQLITE_OK) && (nRow > 0) )
{
printf("%s: %s(%d) – #%d : ID[ %s ] , Name[ %s ] \n",
__FILE__, __FUNCTION__, __LINE__,
recNo, dbResult[nColumn], dbResult[nColumn + 1]);
sqlite3_free_table (dbResult);
} /* end of if( nRow > 0 ) */
else /* nRow == 0 */
sqlite3_free_table (dbResult);
} /* end of for(recNo) */
} /* end of if database is connected successfully */
else
fprintf(stderr, "%s: %s(%d) - Can Not open SQLite3 database [ %s ] \n",
__FILE__, __FUNCTION__, __LINE__, SQLite_DBNAME);
sqlite3_close (db); /* Whether the database is connected successfully or not, it needs to close the
database object for releasing allocated memory. */
SQLite Manager (Firefox Add-Ons)
1. Download and install the latest Firefox web browser
2. Open Firefox and connet this site
https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/
3. Click button to download and install SQLite Manager add-on
4. After installation, invoke SQLite Manager by click menu “Tools -> SQLite Manager”
5. SQLite Manager window is as below.
In SQLite Manager, click “Database -> Connect Database” menu to open File Dialog and locate the SQLite
database file you want to open.
When opening a SQLite database, it would be below screens.
If you want to close the opened database, by clicking “Database -> Close Database” menu.
View
tables
View table’s
schema
View records of
speicified table
It can change field’s
property by right click
and click “Edit” item
It can modify value of
fields of one record by
by right click and click
“Edit” item