C语言操作Sqlite 数据库

前端之家收集整理的这篇文章主要介绍了C语言操作Sqlite 数据库前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

代码

*****************************sqlite.c******************************************

#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,'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,&ErrMsg); if(ret != sqlITE_OK) { fprintf(stderr,"sql Error:%s\n",ErrMsg); sqlite3_free(ErrMsg); } //执行插入记录sql语句 
    ret = sqlite3_exec(db,sql2,&ErrMsg); if(ret !=sqlITE_OK) { printf("插入数据成功\n"); } ret = sqlite3_exec(db,sql3,&ErrMsg); { printf("插入数据成功\n"); } //查询数据表内容 
    printf("查询数据表内容\n"); sqlite3_exec(db,sql4,select_callback,0,&ErrMsg); //关闭数据库 
 sqlite3_close(db); db = 0; printf("数据库关闭成功!\n"); return 0; }

**************************************Makefile**********************************

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
原文链接:https://www.f2er.com/sqlite/201015.html

猜你在找的Sqlite相关文章