sqlite C/C++ API

官网:https://sqlite.org/download.html


下载代码安装三步走:

./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 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 <stdio.h>
#include <stdlib.h>
#include <sqlite3.h>

#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------\n", (const char*)userData);
		for (index=0; index<columnNum; index++) {
			printf("%s\t", columnName[index]);
		}
		printf("\n");
	}
	for(index=0; index<columnNum; index++){
		printf("%s\t", columnText[index] ? columnText[index] : "NULL");
	}
	printf("\n");
	
	return 0;
}

int main(int argc, char* argv[])
{
   sqlite3 *db;
   char *zErrMsg = 0;
   int rc;
   char *sql;

   rc = sqlite3_open(DB_FILENAME, &db);

   if( rc != SQLITE_OK){
      fprintf(stderr, "Can‘t open database: %s\n", sqlite3_errmsg(db));
      exit(0);
   }else{
      fprintf(stderr, "Opened database successfully\n");
   }
   
   sql = "CREATE TABLE COMPANY("         "ID INT PRIMARY KEY  NOT NULL,"		 "NAME           TEXT NOT NULL,"		 "AGE            INT  NOT NULL,"		 "ADDRESS        CHAR(50),"		 "SALARY         REAL);";

	rc = sqlite3_exec(db, sql, callback, (void *)sql, &zErrMsg);
	if(rc != SQLITE_OK){
		fprintf(stderr, "SQL error :%s\n", zErrMsg);
		sqlite3_free(zErrMsg);
	} else {
		fprintf(stdout, "Table created successfully\n");
	}
	
	sql = "INSERT INTO COMPANY(ID, NAME, AGE, ADDRESS, SALARY) "	      "VALUES(1, ‘Paul‘, 32, ‘California‘, 20000.00); "		  "INSERT INTO COMPANY(ID, NAME, AGE, ADDRESS, SALARY) "		  "VALUES(2, ‘Allen‘, 25, ‘Texas‘, 15000.00);"
		  "INSERT INTO COMPANY(ID, NAME, AGE, ADDRESS, SALARY) "		  "VALUES(3, ‘Teddy‘, 23, ‘Norway‘, 20000.00);"
		  "INSERT INTO COMPANY(ID, NAME, AGE, ADDRESS, SALARY) "		  "VALUES(4, ‘Mark‘, 25, ‘Rick-Mond‘, 65000.00);";
	rc = sqlite3_exec(db, sql, callback, (void *)sql, &zErrMsg);
	if(rc != SQLITE_OK){
		fprintf(stderr, "SQL error: %s", zErrMsg);
		sqlite3_free(zErrMsg);
	} else {
		fprintf(stdout, "Records created successfully\n");
	}
	
	sql = "SELECT * FROM COMPANY;";
	rc = sqlite3_exec(db, sql, callback, (void *)sql, &zErrMsg);
	if(rc != SQLITE_OK){
		fprintf(stderr, "SQL error: %s", zErrMsg);
		sqlite3_free(zErrMsg);
	} else {
		fprintf(stdout, "Select records successfully\n");
	}
	
   sqlite3_close(db);
   
   return 0;
}


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


sqlite C/C++ API,布布扣,bubuko.com

sqlite C/C++ API

上一篇:linux下源码搭建php环境之mysql(一)


下一篇:mongoose 数据库操作2