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;}
}文章来源:https://www.toymoban.com/news/detail-859771.htmlsqlite3_close(ppDb);
return 0;
}
文章来源地址https://www.toymoban.com/news/detail-859771.html
到了这里,关于数据库--Sqlite3的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!