linux c說話操作數據庫(銜接sqlite數據庫)。本站提示廣大學習愛好者:(linux c說話操作數據庫(銜接sqlite數據庫))文章只能為提供參考,不一定能成為您想要的結果。以下是linux c說話操作數據庫(銜接sqlite數據庫)正文
#include<stdio.h>
#include<sqlite3.h>
int select_callback(void *data,int col_count,char **col_values,char **col_name)
{
//每筆記錄回調一次該函數,有若干條就回調若干次
int i;
for(i=0;i<col_count;i++)
{
printf("%s=%s\n",col_name[i],col_values[i]==0?"NULL":col_values[i]);
}
return 0;
}
int main(int argc,char **argv)
{
//創立數據庫表
const char *SQL1="create table users(id int PRIMARY KEY,name varchar(20),birthday datetime);";
//向數據庫拔出數據
const char *SQL2="insert into users values(1,'myd','2013-10-9');";
const char *SQL3="insert into users values(2,'myd','2013-10-9');";
//從數據庫查詢數據
const char *SQL4="select * from users;";
char *ErrMsg=0;
int ret = 0;
//銜接數據庫
sqlite3 *db =0;
ret=sqlite3_open("./DataBase",&db);
if(ret != SQLITE_OK)
{
fprintf(stderr,"沒法翻開數據庫:%s",sqlite3_errmsg(db));
return 1;
}
printf("數據庫銜接勝利!\n");
//履行建表
ret = sqlite3_exec(db,SQL1,0,0,&ErrMsg);
if(ret != SQLITE_OK)
{
fprintf(stderr,"SQL Error:%s\n",ErrMsg);
sqlite3_free(ErrMsg);
}
//履行拔出記載SQL語句
ret = sqlite3_exec(db,SQL2,0,0,&ErrMsg);
if(ret !=SQLITE_OK)
{
printf("拔出數據勝利\n");
}
ret = sqlite3_exec(db,SQL3,0,0,&ErrMsg);
{
printf("拔出數據勝利\n");
}
//查詢數據表內容
printf("查詢數據表內容\n");
sqlite3_exec(db,SQL4,select_callback,0,&ErrMsg);
//封閉數據庫
sqlite3_close(db);
db = 0;
printf("數據庫封閉勝利!\n");
return 0;
}
CFLAGS=-L /usr/local/sqlite-autoconf-3070400/lib -I/usr/local/sqlite-autoconf-307040/include -lsqlite3
all: sqlite.o sqlite
sqlite:sqlite.o
gcc sqlite.o -o sqlite $(CFLAGS)
sqlite.o:sqlite.c
gcc -c sqlite.c $(CFLAGS)
clean:
rm -rf sqlite *.o