思维导图
数据库
#include <myhead.h>
//添加学生信息
void insert_stu(sqlite3* sq,char* errMsg)
{char text[128];int ID,age,height;char name[10];scanf("%d%s%d%d",&ID,name,&age,&height);sprintf(text,"insert into Student values (%d,'%s',%d,%d)",ID,name,age,height);sqlite3_exec(sq,text,NULL,NULL,&errMsg);
}
//更新学生信息
void update_stu(sqlite3* sq,char* errMsg)
{char text[128];int IDs;char old[10],new[10];printf("请输入要修改的学生的学号:");scanf("%d",&IDs);printf("请输入要修改的学生的信息:");scanf("%s",old);printf("请输入新的该学生信息:");scanf("%s",new);if(strcmp(old,"name")==0){sprintf(text,"update Student set name='%s' where ID=%d",new,IDs);sqlite3_exec(sq,text,NULL,NULL,&errMsg);printf("修改成功\n");}else if(strcmp(old,"age")==0){sprintf(text,"update Student set age=%s where ID=%d",new,IDs);sqlite3_exec(sq,text,NULL,NULL,&errMsg);printf("修改成功\n");}else if(strcmp(old,"height")==0){sprintf(text,"update Student set height=%s where ID=%d",new,IDs);sqlite3_exec(sq,text,NULL,NULL,&errMsg);printf("修改成功\n");}else{printf("输入不合法\n");}
}
//删除学生
void delete_stu(sqlite3* sq,char* errMsg)
{int id;char text[128];printf("请输入要删除的学生学号:");scanf("%d",&id);sprintf(text,"delete from Student where ID=%d",id);sqlite3_exec(sq,text,NULL,NULL,&errMsg);printf("删除成功\n");
}int main(int argc, char const *argv[])
{//创建数据库sqlite3 *sq = NULL;if(sqlite3_open("./mysq.db",&sq)!=0){printf("打开失败\n");}printf("打开成功\n");//创建学生表char *errMsg = 0;char sql[128]="create table if not exists Student(ID int,name char,age int,height int);";int rc = sqlite3_exec(sq,sql,NULL,NULL,&errMsg);printf("%d\n",rc);if(rc != SQLITE_OK){perror("exec");return -1;} while(1){int a = 0;printf("------1添加学生信息-----\n");printf("------2删除学生信息-----\n");printf("------3修改学生信息-----\n");scanf("%d",&a);switch(a){case 1:insert_stu(sq,errMsg);break;case 2:delete_stu(sq,errMsg);break;case 3:update_stu(sq,errMsg);break;default:printf("请重新输入\n");}}return 0;
}