您的位置:首页 > 数据库

SQLite3 数据库基本操作

2010-06-24 10:33 603 查看
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:

sqlite> .output
gps.sql

<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,
/* 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_prepare16(

sqlite3 *db,
/* Database handle */

const void *zSql,
/* SQL statement, UTF-16 encoded */

int nByte,
/* Maximum length of zSql in
bytes. */

sqlite3_stmt **ppStmt,
/* OUT: Statement handle */

const void **pzTail
/* OUT: Pointer to unused portion of zSql
*/

);

int sqlite3_prepare16_v2(

sqlite3 *db,
/* Database handle */

const void *zSql,

/*
SQL statement, UTF-16 encoded */

int nByte,
/* Maximum length of zSql in
bytes. */

sqlite3_stmt **ppStmt,
/* 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

int sqlite3_close(sqlite3 *);

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: