下載代碼安裝三步走:
./configure // ./configure --help查看安裝參數設置,學習configure的配置,明白安裝後include、lib、bin等文件的位置 make make install
常用函數:
int sqlite3_open( const char *filename, /* Database filename (UTF-8) */ sqlite3 **ppDb /* OUT: SQLite db handle */ ); typedef int (*sqlite3_callback)( void*, /* Data provided in the 4th argument of sqlite3_exec() */ int, /* The number of columns in row */ char**, /* An array of strings representing fields in the row */ char** /* An array of strings representing column names */ ); 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 */ ); int sqlite3_close(sqlite3*);
#include#include #include #define DB_FILENAME /home/suo/Desktop/sqlite/datadir/sqlite.db static int flag = 0; static int callback(void *userData, int columnNum, char **columnText, char **columnName) { int index; if (userData && !flag++) { fprintf(stdout, ---------%s------ , (const char*)userData); for (index=0; index<columnnum; index++)="" {="" printf(%s="" ,="" columnname[index]);="" }="" printf(="" );="" for(index="0;" index<columnnum;="" index++){="" columntext[index]="" ?="" :="" null);="" return="" 0;="" int="" main(int="" argc,="" char*="" argv[])="" sqlite3="" *db;="" char="" *zerrmsg="0;" rc;="" *sql;="" rc="sqlite3_open(DB_FILENAME," &db);="" if(="" !="SQLITE_OK){" fprintf(stderr,="" can't="" open="" database:="" %s="" sqlite3_errmsg(db));="" exit(0);="" }else{="" opened="" database="" successfully="" sql="CREATE" table="" company(="" id="" primary="" key="" not="" null,="" name="" text="" age="" address="" char(50),="" salary="" real);;="" sql,="" callback,="" (void="" *)sql,="" &zerrmsg);="" if(rc="" error="" :%s="" zerrmsg);="" sqlite3_free(zerrmsg);="" else="" fprintf(stdout,="" created="" into="" company(id,="" name,="" age,="" address,="" salary)="" values(1,="" 'paul',="" 32,="" 'california',="" 20000.00);="" insert="" values(2,="" 'allen',="" 25,="" 'texas',="" 15000.00);="" values(3,="" 'teddy',="" 23,="" 'norway',="" values(4,="" 'mark',="" 'rick-mond',="" 65000.00);;="" error:="" %s,="" records="" *="" from="" company;;="" select="" sqlite3_close(db);="" }
對應Makefile:
#! /usr/make rm=/bin/rm -f CC = gcc DEFS = PROGNAME = testsqlite INCLUDES = -I /home/suo/Desktop/sqlite/include DEFINES += $(INCLUDES) $(DEFS) CFLAGS = $(DEFINES) -g -Wall -O2 LDFLAGS = SRCS = testsqlite.c OBJS = testsqlite.o LIBS = -L./lib -lsqlite3 .cpp.o: $(rm) $@ $(CC) $(CFLAGS) -c $*.cpp all: $(PROGNAME) $(PROGNAME) : $(OBJS) $(CC) $(CFLAGS) -o $(PROGNAME) $(OBJS) $(LDFLAGS) $(LIBS) clean: $(rm) $(OBJS) $(PROGNAME) core *~
執行結果:
Opened database successfully Table created successfully Records created successfully ---------SELECT * FROM COMPANY;------ ID NAME AGE ADDRESS SALARY 1 Paul 32 California 20000.0 2 Allen 25 Texas 15000.0 3 Teddy 23 Norway 20000.0 4 Mark 25 Rick-Mond 65000.0 Select records successfully