sqlite3 Database manipulate
<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:Verdana; panose-1:2 11 6 4 3 5 4 4 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:536871559 0 0 0 415 0;} @font-face {font-family:"DejaVu Sans"; mso-font-alt:"MS Gothic"; mso-font-charset:128; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:0 0 0 0 0 0;} @font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"DejaVu Sans Mono"; mso-font-alt:"MS Gothic"; mso-font-charset:128; mso-generic-font-family:modern; mso-font-pitch:auto; mso-font-signature:0 0 0 0 0 0;} @font-face {font-family:"/@DejaVu Sans"; mso-font-charset:128; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:0 0 0 0 0 0;} @font-face {font-family:"/@DejaVu Sans Mono"; mso-font-charset:128; mso-generic-font-family:modern; mso-font-pitch:auto; mso-font-signature:0 0 0 0 0 0;} /* Style Definitions */ p.MsoNormal,li.MsoNormal,div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; mso-pagination:none; mso-hyphenate:none; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"DejaVu Sans"; mso-font-kerning:.5pt; mso-fareast-language:#00FF;} h2 {mso-style-next:正文文本; margin-top:12.0pt; margin-right:0cm; margin-bottom:6.0pt; margin-left:28.8pt; text-indent:-28.8pt; mso-pagination:none; page-break-after:avoid; mso-outline-level:2; mso-list:l0 level2 lfo1; mso-hyphenate:none; tab-stops:list 0cm; font-size:18.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"DejaVu Sans"; mso-bidi-font-family:"DejaVu Sans"; mso-font-kerning:.5pt; mso-fareast-language:#00FF;} p.MsoHeader,li.MsoHeader,div.MsoHeader {margin:0cm; margin-bottom:.0001pt; mso-pagination:no-line-numbers; mso-hyphenate:none; tab-stops:center 249.3pt right 498.6pt; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"DejaVu Sans"; mso-font-kerning:.5pt; mso-fareast-language:#00FF;} p.MsoBodyText,li.MsoBodyText,div.MsoBodyText {margin-top:0cm; margin-right:0cm; margin-bottom:6.0pt; margin-left:0cm; mso-pagination:none; mso-hyphenate:none; font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"DejaVu Sans"; mso-font-kerning:.5pt; mso-fareast-language:#00FF;} p.PreformattedText,li.PreformattedText,div.PreformattedText {mso-style-name:"Preformatted Text"; margin:0cm; margin-bottom:.0001pt; mso-pagination:none; mso-hyphenate:none; font-size:10.0pt; font-family:"DejaVu Sans Mono"; mso-hansi-font-family:"DejaVu Sans Mono"; mso-bidi-font-family:"DejaVu Sans Mono"; mso-font-kerning:.5pt; mso-fareast-language:#00FF;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:612.0pt 792.0pt; margin:2.0cm 2.0cm 2.0cm 2.0cm; mso-header-margin:36.0pt; mso-footer-margin:36.0pt; mso-page-numbers:num-in-dash; mso-paper-source:0;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:1; mso-list-template-ids:1;} @list l0:level1 {mso-level-number-format:none; mso-level-suffix:none; mso-level-text:""; mso-level-tab-stop:0cm; mso-level-number-position:left; margin-left:21.6pt; text-indent:-21.6pt;} @list l0:level2 {mso-level-number-format:none; mso-level-suffix:none; mso-level-text:""; mso-level-tab-stop:0cm; mso-level-number-position:left; margin-left:28.8pt; text-indent:-28.8pt;} @list l0:level3 {mso-level-number-format:none; mso-level-suffix:none; mso-level-text:""; mso-level-tab-stop:0cm; mso-level-number-position:left; margin-left:36.0pt; text-indent:-36.0pt;} @list l0:level4 {mso-level-number-format:none; mso-level-suffix:none; mso-level-text:""; mso-level-tab-stop:0cm; mso-level-number-position:left; margin-left:43.2pt; text-indent:-43.2pt;} @list l0:level5 {mso-level-number-format:none; mso-level-suffix:none; mso-level-text:""; mso-level-tab-stop:0cm; mso-level-number-position:left; margin-left:50.4pt; text-indent:-50.4pt;} @list l0:level6 {mso-level-number-format:none; mso-level-suffix:none; mso-level-text:""; mso-level-tab-stop:0cm; mso-level-number-position:left; margin-left:57.6pt; text-indent:-57.6pt;} @list l0:level7 {mso-level-number-format:none; mso-level-suffix:none; mso-level-text:""; mso-level-tab-stop:0cm; mso-level-number-position:left; margin-left:64.8pt; text-indent:-64.8pt;} @list l0:level8 {mso-level-number-format:none; mso-level-suffix:none; mso-level-text:""; mso-level-tab-stop:0cm; mso-level-number-position:left; margin-left:72.0pt; text-indent:-72.0pt;} @list l0:level9 {mso-level-number-format:none; mso-level-suffix:none; mso-level-text:""; mso-level-tab-stop:0cm; mso-level-number-position:left; margin-left:79.2pt; text-indent:-79.2pt;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} -->
Table of Contents
1. Database basics – sql
2. The core C API – basics
1. Database basics
<1> Creating a database that we will call gps.db:
# sqlite3 gps.db
sqlite version 3.6.23.1
Enter ".help" for instructions
Enter sql statements terminated with a ";"
sqlite> .exit
<2> Now you have a database file on disk called gps.db
# ls
gps.db test
<3> Create a table. Issue the following statement from the shell:
sqlite> create table pos(pos_x varchar(10),pos_y varchar(10));
<4> Add a few rows to the table:
sqlite> insert into pos values('234.2','678.8');
sqlite> insert into pos values('824.5','926.7');
sqlite> insert into pos values('176.5','278.0');
<5> Now fetch them back:
sqlite> .headers on
sqlite> select * from pos;
pos_x|pos_y
234.2|678.8
824.5|926.7
176.5|278.0
<5> Getting table information:
sqlite> .tables
pos
sqlite> .schema
CREATE TABLE pos(pos_x varchar(10),pos_y varchar(10));
sqlite> .indices
<6> Exporting Data:
<7> View configure information:
sqlite> .show
echo: off
explain: off
headers: on
mode: list
nullvalue: ""
output: stdout
separator: "|"
width:
2. The core C API
<1>some basics API
opening A New Database Connection
int sqlite3_open(
const char *filename, /* Database filename (UTF-8) */
sqlite3 **ppDb /* OUT: sqlite db handle */
);
int sqlite3_open16(
const void *filename, /* Database filename (UTF-16) */
sqlite3 **ppDb /* OUT: sqlite db handle */
);
int sqlite3_open_v2(
const char *filename, /* Database filename (UTF-8) */
sqlite3 **ppDb, /* OUT: sqlite db handle */
int flags, /* Flags */
const char *zVfs /* Name of VFS module to use */
);
Compiling An sql Statement
int sqlite3_prepare(
sqlite3 *db, /* Database handle */
const char *zsql, /* sql statement,UTF-8 encoded */
int nByte, /* Maximum length of zsql in bytes. */
sqlite3_stmt **ppStmt, /* OUT: Statement handle */
const char **pzTail /* OUT: Pointer to unused portion of zsql */
);
int sqlite3_prepare_v2(
sqlite3 *db, /* OUT: Statement handle */
const char **pzTail /* OUT: Pointer to unused portion of zsql */
);
int sqlite3_prepare16(
sqlite3 *db, /* Database handle */
const void *zsql,UTF-16 encoded */
int nByte, /* OUT: Statement handle */
const void **pzTail /* OUT: Pointer to unused portion of zsql */
);
int sqlite3_prepare16_v2(
sqlite3 *db, /* sql statement, /* OUT: Statement handle */
const void **pzTail /* OUT: Pointer to unused portion of zsql */
);
Destroy A Prepared Statement Object
int sqlite3_finalize(sqlite3_stmt *pStmt);
Closing A Database Connection
Convenience Routines For Running Queries
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 written here */
int *pnColumn, /* Number of result columns written here */
char **pzErrmsg /* Error msg written here */
);
void sqlite3_free_table(char **result);
One-Step Query Execution Interface
int sqlite3_exec(
sqlite3*, /* An open database */
const char *sql, /* sql to be evaluated */
int (*callback)(void*,int,char**,char**), /* Callback function */
void *, /* 1st argument to callback */
char **errmsg /* Error msg written here */
);
<2>program
Compile arm format file:
arm-linux-gcc -I/home/furtherchan/work/sqlite_porting/sqlite_build/include -L/home/furtherchan/work/sqlite_porting/sqlite_build/lib -o c_api_sqlite c_api_sqlite.c -lsqlite3
furtherchan@further:~/work/sqlite_porting$ ll c_api_sqlite
-rwxr-xr-x 1 furtherchan furtherchan 9488 2010-06-23 15:25 c_api_sqlite
furtherchan@further:~/work/sqlite_porting$ arm-linux-strip c_api_sqlite
furtherchan@further:~/work/sqlite_porting$ ll c_api_sqlite
-rwxr-xr-x 1 furtherchan furtherchan 4040 2010-06-23 15:26 c_api_sqlite