#include <my_head.h>
sqlite3 *init();
void func();
void insert(sqlite3 *my_db);
void modify(sqlite3 *my_db);
void delect(sqlite3 *my_db);
void watch(sqlite3 *my_db);
int callback(void* arg ,int ncolumn ,char** f_value,char** f_name);
int flag=0;
int sqlite3_get_table(sqlite3 *db, const char *sql, char ***pazResult,int *pnRow, int *pnColumn, char **errmsg);
//查看数据 使用查看函数int sqlite3_get_table(sqlite3 *db, const char *sql, char ***pazResult,\
int *pnRow, int *pnColumn, char **errmsg);
void watch(sqlite3 *my_db){
char sqlbuff[128]="select * from student";
char **result=NULL;
int line=0;
int column=0;
char *errmsg=NULL;
if(SQLITE_OK != sqlite3_get_table(my_db,sqlbuff,&result,&line,&column,&errmsg)){
printf("%s %s %d\n",__FILE__,__func__,__LINE__);
printf("%s\n",errmsg);
sqlite3_free(errmsg);
exit(-1);
}
sqlite3_free(errmsg);
int i=0;
int j=0;
for(i=0;i<column;i++){
printf("%15s",result[i]);
}
printf("\n");
for(i=0;i<line;i++){
for(j=0;j<column;j++){
printf("%15s",result[(i+1)*column+j]);
}
printf("\n");
}
sqlite3_free_table(result);
printf("查询成功..\n");
return;
}
//回调函数
#if 0
int callback(void *arg ,int ncolumn ,char **f_value,char **f_name){
int i=0;
if(0 == flag){
for(i=0;i<ncolumn;i++){
printf("%15s",f_name[i]);
}
printf("\n");
flag=1;
}
for(i=0;i<ncolumn;i++){
printf("%15s",f_value[i]);
}
printf("\n");
return 0;
}
#endif
#if 0
//查看数据,查看所有 使用回调函数
void watch(sqlite3 *my_db){
if(my_db == NULL){
printf("请检查入参..\n");
exit(-1);
}
char sqlbuff[128]={0};
char *errmsg=NULL;
int id=0;
sprintf(sqlbuff,"select * from student");
flag=0;
if(SQLITE_OK != sqlite3_exec(my_db,sqlbuff,callback,NULL,&errmsg)){
printf("%s %s %d\n",__FILE__,__func__,__LINE__);
printf("%s\n",errmsg);
sqlite3_free(errmsg);
exit(-1);
}
sqlite3_free(errmsg);
printf("删除成功..\n");
return;
}
#endif
//根据id删除数据
void delect(sqlite3 *my_db){
if(my_db == NULL){
printf("请检查入参..\n");
exit(-1);
}
char sqlbuff[128]={0};
char *errmsg=NULL;
int id=0;
printf("请输入想要删除的学生的id:");
scanf("%d",&id);
sprintf(sqlbuff,"delete from student where id=%d",id);
if(SQLITE_OK != sqlite3_exec(my_db,sqlbuff,NULL,NULL,&errmsg)){
printf("%s %s %d\n",__FILE__,__func__,__LINE__);
printf("%s\n",errmsg);
sqlite3_free(errmsg);
exit(-1);
}
sqlite3_free(errmsg);
printf("删除成功..\n");
return;
}
//根据id修改元素
void modify(sqlite3 *my_db){
if(my_db == NULL){
printf("请检查入参..\n");
exit(-1);
}
int id=0;
printf("请输入想要修改的学生的id:");
scanf("%d",&id);
char sqlbuff[128]={0};
int score=0;
char name[12]={0};
char *errmsg=NULL;
printf("请输入想要修改的值name和score:");
scanf("%s%d",name,&score);
sprintf(sqlbuff,"update student set name='%s',score=%d where id=%d",\
name,score,id);
if(SQLITE_OK != sqlite3_exec(my_db,sqlbuff,NULL,NULL,&errmsg)){
printf("%s %s %d\n",__FILE__,__func__,__LINE__);
printf("%s\n",errmsg);
sqlite3_free(errmsg);
exit(-1);
}
sqlite3_free(errmsg);
printf("修改成功..\n");
return;
}
//插入元素
void insert(sqlite3 *my_db){
if(my_db == NULL){
printf("请检查入参..\n");
exit(-1);
}
int ret=0;
char sqlbuff[128]={0};
int id=0;
int score=0;
char name[12]={0};
char *errmsg=NULL;
printf("请输入想要插入的学生信息:");
scanf("%d%s%d",&id,name,&score);
printf("111\n");
sprintf(sqlbuff,"insert into student values(%d,'%s',%d)",id,name,score);
if(SQLITE_OK != sqlite3_exec(my_db,sqlbuff,NULL,NULL,&errmsg)){
printf("%s %s %d\n",__FILE__,__func__,__LINE__);
printf("%s",errmsg);
sqlite3_free(errmsg);
exit(-1);
}
sqlite3_free(errmsg);
printf("插入成功..\n");
return;
}
//初始化——建表
sqlite3 *init(){
sqlite3 *my_db=NULL;
int ret=0;
if(SQLITE_OK != sqlite3_open("jjjj.db",&my_db)){
printf("错误信息:%s\n",sqlite3_errmsg(my_db));
exit(-1);
}
char sqlbuff[128]={0};
char *s="create table if not exists student(id int primary key,name char,score char)";
char *errmsg=NULL;
sprintf(sqlbuff,"%s",s);
if(SQLITE_OK != sqlite3_exec(my_db,sqlbuff,NULL,NULL,&errmsg)){
printf("%s %s %d\n",__FILE__,__func__,__LINE__);
printf("错误码:%d 错误信息:%s\n",ret,errmsg);
sqlite3_free(errmsg);
exit(-1);
}
printf("建表成功..\n");
return my_db;
}
void func(){
printf("***************************************\n");
printf("***1.添加 2.修改 3.删除 4.查看 5.退出***\n");
printf("***************************************\n");
}
int main(int argc, const char *argv[]){
//创建数据库表
sqlite3 *my_db=NULL;
my_db=init();
int choose=0;
while(1){
func();
printf("请输入您的操作:");
scanf("%d",&choose);
switch(choose){
case 1:
insert(my_db);
break;
case 2:
modify(my_db);
break;
case 3:
delect(my_db);
break;
case 4:
watch(my_db);
break;
}
if(5 == choose){
break;
}
}
sqlite3_close(my_db);
return 0;
}
使用c语言+sqlite3实现简单的学生管理系统(Linux可运行)
最新推荐文章于 2024-05-25 14:49:08 发布