此程序使用sqlite库编写,编译时需要sqlite3的源码库函数和头文件,编译连接时需加上:-lpthread -ldl 选项。
(下载地址:http://download.csdn.net/detail/hanbo622/7977201)
main.c
#include <stdio.h> #include <string.h> #include <stdlib.h> #include "stu_sqlite.h" #include "sqlite3.h" #define PRINT printf(" 《学生信息管理系统》:"); sqlite3 *db = NULL; char *help ="*************************************\n"\ "*add: add student information *\n"\ "*del: del student information *\n"\ "*order: order student information *\n"\ "*find: find student information *\n"\ "*update: find student information *\n"\ "*print: print student information *\n"\ "*cls: clear screen information *\n"\ "*help: display help information *\n"\ "*exit: exit *\n"\ "*************************************\n"; void add_fun(void) { STU stu; printf("Please input student (id name city @R_403_182@):"); scanf("%d %s %s %d",&stu.id,stu.name,stu.city,&stu.@R_403_182@); insert_stu_db(db,&stu); } void del_fun(void) { STU stu; printf("*****************************************************\n"); printf("*id: delete (id=1) student information *\n"); printf("*name: delete (name='hanbo') student information *\n"); printf("*city: delete (city='beijing') student information *\n"); printf("*@R_403_182@: delete (@R_403_182@=100) student information *\n"); printf("*****************************************************\n"); printf("Please input student type:"); scanf("%s",stu.type); delete_stu_db(db,&stu); } void order_fun(void) { STU stu; printf("***********************************\n"); printf("*id: In lin with id order *\n"); printf("*name: In lin with name order *\n"); printf("*city: In lin with city order *\n"); printf("*@R_403_182@: In lin with @R_403_182@ order *\n"); printf("***********************************\n"); printf("Please input student type:"); scanf("%s",stu.type); order_by_id_stu_db_exec(db,&stu); } void find_fun(void) { STU stu; printf("****************************************\n"); printf("*and/or: id=1 and/or addr='beijing' *\n"); printf("*between: id between 1 and 3 *\n"); printf("*like: addr like '%%jing%%' *\n"); printf("*not: id not in (1) *\n"); printf("****************************************\n"); printf("Please input find type:"); scanf("%s",stu.type); find_stu_db(db,&stu); } void update_fun(void) { STU stu; int i=0; printf("**********************************************************\n"); printf("* id=1,addr='beijing' where name='perter' *\n"); printf("*name peter's information update id=1 and addr='beijing' *\n"); printf("**********************************************************\n"); printf("Please input find type:"); getchar(); stu.type[i]=getchar(); while(stu.type[i]!='\n') { i++; stu.type[i]=getchar(); } stu.type[i]='\0'; update_stu_db(db,&stu); } void print_fun(void) { show_stu_info(db); } void cls_fun(void) { system("clear"); } void exit_fun(void) { sqlite3_close(db); exit(0); } void help_fun(void ) { printf("%s\n",help); } typedef void (*FUN)(void); typedef struct cmd { char *cmd; FUN fun; }CMD; CMD cmd_list[] = { {"add",add_fun},{"del",del_fun},{"order",order_fun},{"find",find_fun},{"update",update_fun},{"print",print_fun},{"cls",cls_fun},{"exit",exit_fun},{"help",help_fun} }; int main(int argc,char **argv) { char cmdbuf[20]; char *stu_db = "stu_info.db"; int i = 0; db = (sqlite3 *)create_db(stu_db); //创建数据库 create_db_table(db); //创建表头 id name @R_403_182@ PRINT; while(1) { scanf("%s",cmdbuf); for(i=0;i<9;i++) { if(!strcmp(cmdbuf,cmd_list[i].cmd)) { cmd_list[i].fun(); PRINT; } } } return 0; }
stu_sqlite.c
#include <stdio.h> #include <stdlib.h> #include "stu_sqlite.h" #include "sqlite3.h" void sqlite3_get_table_func(sqlite3 *db,char *sql) { char *errormsg=NULL; char **resultp=NULL; int nrow=0; int ncolumn=0; int ret=sqlite3_get_table(db,sql,&resultp,&nrow,&ncolumn,&errormsg); if(ret!=sqlITE_OK) { printf("errormsg=%s\n",errormsg); } else { int i=0; int j=0; for(i=0;i<=nrow;i++) { if(i==0) printf(" %-3s %-10s %-10s %-3s \n",resultp[0],resultp[1],resultp[2],resultp[3]); else if(i==1) { printf("************************************\n"); printf("* %-3s %-10s %-10s %-3s *\n",resultp[0+i*ncolumn],resultp[1+i*ncolumn],resultp[2+i*ncolumn],resultp[3+i*ncolumn]); } else printf("* %-3s %-10s %-10s %-3s *\n",resultp[3+i*ncolumn]); } printf("************************************\n\n"); } } sqlite3 *create_db(char *db_name) { sqlite3 *db; int ret=sqlite3_open(db_name,&db); if(ret!=sqlITE_OK) { perror("sqlite3_open"); } system("chmod 777 stu_info.db"); return db; } void create_db_table(sqlite3 *db) { char *sql="create table persons (id integet,name text,city text,@R_403_182@ integet);"; char *errormsg; int ret=sqlite3_exec(db,NULL,errormsg); } } void insert_stu_db(sqlite3 *db,STU *stu) { char sql[100]=""; sprintf(sql,"insert into persons values (%d,'%s',%d);",stu->id,stu->name,stu->city,stu->@R_403_182@); char *errormsg; int ret=sqlite3_exec(db,errormsg); } } void delete_stu_db(sqlite3 *db,STU *stu) { char sql[100]=""; char *errormsg; sprintf(sql,"delete from persons where %s;",stu->type); int ret=sqlite3_exec(db,errormsg); } } void show_stu_info(sqlite3 *db) { char *sql="select * from persons;"; sqlite3_get_table_func(db,sql); } void find_stu_db(sqlite3 *db,STU *stu) { char sql[100]=""; sprintf(sql,"select * from persons where %s;",stu->type); sqlite3_get_table_func(db,sql); } void update_stu_db(sqlite3 *db,"update persons set %s;",errormsg); } } void order_by_id_stu_db_exec(sqlite3 *db,"select * from persons order by %s;",sql); }
stu_sqlite.h
#ifndef __STU_sqlITE_H__ #define __STU_sqlITE_H__ #include "sqlite3.h" typedef struct student_info { int id; char name[10]; char city[20]; int @R_403_182@; char type[100]; }STU; extern sqlite3 *create_db(char *db_name); extern void create_db_table(sqlite3 *db); extern void insert_stu_db(sqlite3 *db,STU *stu); extern void delete_stu_db(sqlite3 *db,STU *stu); extern void order_by_id_stu_db_exec(sqlite3 *db,STU *stu); extern void find_stu_db(sqlite3 *db,STU *stu); extern void update_stu_db(sqlite3 *db,STU *stu); extern void show_stu_info(sqlite3 *db); #endif