#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<sqlite3.h>
int do_insert(sqlite3 *db)//插入
{
int id;
char name[20];
float score ;
printf("请输入id\n");
scanf("%d",&id);
while(getchar()!=10);//吸收垃圾字符
printf("请输入名字\n");
scanf("%s",name);
while(getchar()!=10);//吸收垃圾字符
printf("请输入分数\n");
scanf("%f",&score);
while(getchar()!=10);//吸收垃圾字符
char sql[128]="";
sprintf(sql,"insert into stu values(%d,\"%s\",%g);",id,name,score);
char *errmsg=NULL;//一级指针
//执行一条sql语句
//参数1 数据库句柄指针
//参数2 要被执行1的sql语句
//参数3 函数指针
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
{
fprintf(stderr,"line %d sqlite3_exec %s\n",__LINE__,errmsg);//静态存储区的空间
return -1;
}
printf("增加成功\n");
return -1;
}
int do_delete(sqlite3 *db)//删除记录
{
int id;
printf("请输入id\n");
scanf("%d",&id);
while(getchar()!=10);//吸收垃圾字符
char sql[128]="";
sprintf(sql,"delete from stu where(id=%d);",id);
char *errmsg=NULL;//一级指针
//执行一条sql语句
//参数1 数据库句柄指针
//参数2 要被执行1的sql语句
//参数3 函数指针
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
{
fprintf(stderr,"line %d sqlite3_exec %s\n",__LINE__,errmsg);//静态存储区的空间
return -1;
}
printf("删除成功\n");
return -1;
}
int do_update(sqlite3*db)//修改记录
{
int id;
char name[20];
float score ;
printf("请输入id\n");
scanf("%d",&id);
while(getchar()!=10);//吸收垃圾字符
printf("请输入名字\n");
scanf("%s",name);
while(getchar()!=10);//吸收垃圾字符
printf("请输入分数\n");
scanf("%f",&score);
while(getchar()!=10);//吸收垃圾字符
char sql[128]="";
sprintf(sql,"update stu set name=\"%s\", score=%f where id=%d;",name,score,id);
char *errmsg=NULL;//一级指针
//执行一条sql语句
//参数1 数据库句柄指针
//参数2 要被执行1的sql语句
//参数3 函数指针
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
{
fprintf(stderr,"line %d sqlite3_exec %s\n",__LINE__,errmsg);//静态存储区的空间
return -1;
}
printf(" 修改成功\n");
return -1;
}
int main(int argc, const char *argv[])
{
//如果数据库不存在,则创建后打开
//如果存在,则直接打开
sqlite3* db=NULL;
if(sqlite3_open("./my.db",&db)!=SQLITE_OK)//SQLITE_OK就是0;
{
printf("linde %d sqlite3_open failed[%d]%s\n ",__LINE__,sqlite3_errcode(db),sqlite3_errmsg(db));
return -1;
}
printf("sqlite3_open success\n");
//创建一个表
//注意:c代码编写的sql预计和数据中编写一致
char sql[128]="create table if not exists stu(id int,name char,score float)";
char *errmsg=NULL;//一级指针
//执行一条sql语句
//参数1 数据库句柄指针
//参数2 要被执行1的sql语句
//参数3 函数指针
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
{
fprintf(stderr,"line %d sqlite3_exec %s\n",__LINE__,errmsg);//静态存储区的空间
return -1;
}
printf("create table stu success\n");
char c=0;
while(1)
{
system("clear");
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);//吸收垃圾字符
switch(c)
{
case '1':
do_insert(db);
break;
case '2':
do_delete(db);
break;
case '3':
do_update(db);
break;
case '4':
//do_select();
break;
case '5':
goto END;
break;
default:
printf("输入错误,请重新输入\n");
}
}
END:
//关闭数据库,释放其内存中的空间
if(sqlite3_close(db)!=SQLITE_OK)
{
printf("line %d sqlite3_close failed%s\n",__LINE__,sqlite3_errmsg(db));
return -1;
}
db=NULL;
return 0;
}