赞
踩
1、思维导图
2sqlite3在linux中是实现数据的增删,改
#include<myhead.h>
int main(int argc, const char *argv[])
{
//1、定义一个数据库句柄指针
sqlite3* ppDb =NULL;
//2、创建或打开数据库
if(sqlite3_open("./mydb.db",&ppDb)!=SQLITE_OK)
{
printf("sqlite3_open error\n");
return -1;
}
printf("数据库成功打开\n");
//
//3、创建数据表
//3.1、准备sql语句
char sql[128]="create table if not exists student(student_id int ,name char,sex char ,score double);";
char *errmsg =NULL;
if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
{
printf("%s\n",errmsg);
return -1;
}
printf("数据表创建成功\n");
int a=0;int a1=0;
char a2[20]="";
char a3[5]="";
int a4=0;
printf("*****学生信息***********\n");
printf("*****1、添加学生信息****\n");
printf("*****2、修改学生信息*****\n");
printf("*****3、删除学生信息*****\n");
printf("*****4、查找学生信息******\n");
printf("*****0、退出系统**********\n");
printf("请输入》》》:");
scanf("%d",&a);switch(a)
{
case 1:
{
printf("请输入信息:");
scanf("%d %s %s %d",&a1,a2,a3,&a4);
getchar();
sprintf(sql,"insert into student values(%d,\"%s\",\"%s\",%d);",a1,a2,a3,a4);
if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
{
printf("%s\n",errmsg);
return -1;
}
break;}
case 2:
{
printf("请输入学号和姓名:");
scanf("%d %s %s %d",a1,a2,a3,a4);
getchar();
sprintf(sql,"UPDATE TABLE student student_id=%d AND name=\"%s\" WHERE sex=\"%s\" %d;",a1,a2,a3,a4);
if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
{
printf("%s\n",errmsg);
return -1;
}
break;
}
case 3:
{
printf("请输入信息:");
scanf("%s",a2);
getchar();
sprintf(sql,"DELETE FROM student WHERE name=\"%s\"",a2);
if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
{
printf("%s\n",errmsg);
return -1;
}
break;
}case 0:
{
sprintf(sql,"DROP TABLE student;");
if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
{
printf("%s\n",errmsg);
return -1;
}
break;}
}sqlite3_close(ppDb);
return 0;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。