#include<stdio.h>
#include<sqlite3.h>
//使用函数前必须声明
//插入,
int do_insert(sqlite3* db);
int main(int argc, const char *argv[])
{
//创建并打开一个数据库
sqlite3* db = NULL;
if(sqlite3_open("./stu.db",&db)!=SQLITE_OK)
{
//打开失败的错误提示:包含两个函数,显示错误信息和错误码
fprintf(stderr,"%d sqlite3_open:%s\n",__LINE__,sqlite3_errmsg(db));
fprintf(stderr,"error_code:%d\n",sqlite3_errcode(db));
return -1;
}
//创建一张表
char sql[128]="create table if not exists stu (id int,name char,score float)";
char *errmsg=NULL;
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
{
fprintf(stderr,"%d__sqlite3_exec:%s\n",__LINE__,errmsg);
return -1;
}
printf("create table stu success\n");
char c=0;
while(1)
{
printf("----------------------\n");
printf("-------1.插入---------\n");
printf("-------2.删除---------\n");
printf("-------3.修改---------\n");
printf("-------4.查询---------\n");
printf("-------5.退出---------\n");
printf("----------------------\n");
printf("请输入>>>");
c=getchar();
while(getchar()!=10);
int s;
switch(c)
{
case '1':
s=do_insert(db);
break;
case '2':
// do_delete();
break;
case '3':
// do_update();
break;
case '4':
// do_select();
break;
case '5':
goto END;
default:
printf("输入错误,请重新输入!!");
}
}
END:
//关闭
if(sqlite3_close(db)!=SQLITE_OK)
{
fprintf(stderr,"%d sqlite_close:%s\n",__LINE__,sqlite3_errmsg(db));
fprintf(stderr,"error_code:%d\n",sqlite3_errcode(db));
return -1;
}
printf("sqlite3 close success\n");
return 0;
}
int do_insert(sqlite3* db)
{
char data[128]="insert into stu values (1,'ld',97)";
char *errmsg=NULL;
if(sqlite3_exec(db,data,NULL,NULL,&errmsg)!=SQLITE_OK)
{
fprintf(stderr,"%d__sqlite3_exec:%s\n",__LINE__,errmsg);
return -1;
}
return 0;
}