网络编程 DAY7作业

1.编程实现数据库增删改查操作

#include 
int do_add(sqlite3 *ppDb)
{
	//1.准备sql语句
	int add_numb = 0;
	char add_name[20] = "";
	char add_sex[5] = "";
	double add_socre = 0;

	printf("请输入学生的学号:");
	scanf("%d",&add_numb);
	printf("请输入学生的姓名:");
	scanf("%s",add_name);
	printf("请输入学生的性别:");
	scanf("%s",add_sex);
	printf("请输入学生的成绩:");
	scanf("%2lf",&add_socre);
	getchar();

	char sql[128] ="";
	sprintf(sql,"insert into Stu values(%d,\"%s\",\"%s\",%2lf);",add_numb,add_name,add_sex,add_socre);


	//2.定义错误信息存放指针
	char *errmsg = NULL;

	//3.执行语句
	if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg) != SQLITE_OK)
	{
		printf("exec error:%s\n",errmsg);

		sqlite3_free(errmsg);
		errmsg = NULL;
		return -1;
	}
	printf("do_add success\n");
	return 0;
}
int do_delete(sqlite3 *ppDb)
{
	//1.准备sql语句
	int delete_numb;
	printf("请输入要删除记录的学号:");
	scanf("%d",&delete_numb);
	getchar();

	char sql[128]="";
	sprintf(sql,"delete from Stu where numb=%d",delete_numb);


	
	//2.定义错误信息存放指针
	char *errmsg = NULL;

	//3.执行语句
	if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg) != SQLITE_OK)
	{
		printf("exec error:%s\n",errmsg);

		sqlite3_free(errmsg);
		return -1;
	}
	printf("do_delete success\n");
	return 0;

}

int do_update(sqlite3*ppDb)
{
	//1.准备sql语句
	char update_field[20] = "";   //要修改的字段
	int key_numb = 0;    //学号关键词的值
	char sql[128] ="";

	printf("请输入要修改的字段:");
	scanf("%s",update_field);
	printf("请输入要修改的值:");
	if(strcmp(update_field,"numb") == 0||strcmp(update_field,"score") == 0)
	{
		int values= 0 ;
		scanf("%d",&values);
		printf("请输入修改信息学生的学号:");
		scanf("%d",&key_numb);
		getchar();
		sprintf(sql,"update Stu set %s=%d where numb=%d;",update_field,values,key_numb);
	}else
	{
		char values[20]= "";
		scanf("%s",values);
		printf("请输入修改信息学生的学号:");
		scanf("%d",&key_numb);
		getchar();
		sprintf(sql,"update Stu set %s=\"%s\" where numb=%d;",update_field,values,key_numb);

	}
		
	//2.定义错误信息存放指针

	char *errmsg = NULL;

	//3.执行语句
	if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg) != SQLITE_OK)
	{
		printf("exec error:%s\n",errmsg);

		sqlite3_free(errmsg);
		return -1;
	}
	printf("do_update success\n");

	return 0;
}
int callback(void* arg,int cols,char **colum_text,char **colum_name)
{
	if(*(int *)arg == 0)
	{
		for(int i = 0;i

你可能感兴趣的:(网络)