Your SlideShare is downloading. ×
Notes for SQLite3 Usage
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Notes for SQLite3 Usage

910
views

Published on

Some notes of using SQLite3 C APIs and relevant tool.

Some notes of using SQLite3 C APIs and relevant tool.

Published in: Technology

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
910
On Slideshare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Notes for SQLite3 Usage William.L wiliwe@gmail.com 2014-03-16
  • 2. 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
  • 3. 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
  • 4. 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 .
  • 5. 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
  • 6. 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.
  • 7. 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 */
  • 8. 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 */
  • 9. } 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 */
  • 10. 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. */
  • 11. 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”
  • 12. 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.
  • 13. 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