sqlite C/C++ API

2014-11-24 12:22:42 · 作者: · 浏览: 1

下载代码安装三步走:

./configure		// ./configure --help查看安装参数设置,学习configure的配置,明白安装后include、lib、bin等文件的位置
make 
make install

学习SQL基本语法,各个 数据库基本相同http://www.w3cschool.cc/sqlite/sqlite-tutorial. html

常用函数:

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 eva luated */
  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 

对应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