程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> sqlite C/C++ API

sqlite C/C++ API

編輯:C++入門知識

 

下載代碼安裝三步走:

 

./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 
#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


 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved