Linux + C + sqlite 3 간단 한 학생 관리 시스템

#include<stdio.h>
#include<stdlib.h>
#include<sqlite3.h>
#include<string.h>
char *errmsg;

void insert_data(sqlite3 *db)
{
	int number,age,score;
	char name[20];

	printf("input the number: ");
	scanf("%d",&number);
	getchar();
	printf("input the name: ");
	scanf("%s",name);
	getchar();
	printf("input the age: ");
	scanf("%d",&age);
	getchar();
	printf("input the score ");
	scanf("%d",&score);
	getchar();

	char *sql=sqlite3_mprintf("insert into student values('%d','%s','%d','%d')",number,name,age,score);

	if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK)
	{
		perror("sqlite3_exec");
		exit(-1);
	}
	else
		printf("insert success!!
"); return; } void delete_data(sqlite3 *db) { int num; printf("please input the number you want to delete
"); scanf("%d",&num); getchar(); char *sql=sqlite3_mprintf("delete from student where number ='%d'",num); if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK) { perror("sqlite3_exec_delete"); exit(-1); } else printf("delete success!!
"); return; } void updata_data(sqlite3 * db) { int num,age,score; char name[20]; printf("please input the number you want to updata
"); scanf("%d",&num); getchar(); printf("input the name: "); scanf("%s",name); getchar(); printf("input the age: "); scanf("%d",&age); getchar(); printf("input the score "); scanf("%d",&score); getchar(); // char sql[1024]; // sprintf(sql,"update student set name='%s',age='%d',score='%d' where number='%d'",name,age,score,num); char *sql=sqlite3_mprintf("update student set name='%s',age='%d',score='%d' where number='%d'",name,age,score,num); if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK) { perror("sqlite3_exec_update"); exit(-1); } else printf("update success!!
"); return; } void show_data(sqlite3 *db) { char ** resultp; int nrow,ncolumn,i,j,index; char *sql="select * from student"; if(sqlite3_get_table(db,sql,&resultp,&nrow,&ncolumn,&errmsg) != SQLITE_OK) { perror("sqlite3_get_table"); exit(-1); } index = ncolumn; for(i = 0 ;i< ncolumn; i++) printf("%s\t",resultp[i]); printf("
"); for(i = 0;i< nrow;i++) { for(j = 0;j< ncolumn;j++) printf("%s\t",resultp[index++]); printf("
"); } return ; } void quit(sqlite3 *db) { printf("BYBYE!!
"); sqlite3_close(db); exit(0); } int main() { int nu; sqlite3 *db; if(sqlite3_open("my.db",&db) != SQLITE_OK) { perror("sqlite3_open"); exit(-1); } while(1) { printf("**********************
"); printf("* 1.insert data *
"); printf("* 2.delete data *
"); printf("* 3.updata data *
"); printf("* 4.show data *
"); printf("* 5. quit *
"); printf("**********************
"); printf("please input the number you want to operate:
"); scanf("%d",&nu); getchar(); switch(nu) { case 1: insert_data(db); break; case 2: delete_data(db); break; case 3: updata_data(db); break; case 4: show_data(db); break; case 5 : quit(db); break; } } return 0; }

좋은 웹페이지 즐겨찾기