代码编织梦想

 

#include <stdio.h>
#include <sqlite3.h>
#include <stdlib.h>
#include <string.h>
void do_insert(sqlite3 *db,FILE *fp);
void do_delete(sqlite3 *db);
void do_select(sqlite3 *db);
void do_alter(sqlite3 *db);
int main(int argc, const char *argv[])
{
	FILE * fp=fopen("./dict.txt","r");
	if(fp < 0)
	{
		perror("fp");
		return -1;
	}
	sqlite3 *db = NULL;
	int  fd=sqlite3_open("./my2.db",&db);
	if(fd != 0)
	{
		printf("error\n");
		return -1;
	}
	printf("open success\n");
	//创建表格
	char *errmsg = NULL;
	char sql[128]="create table if not exists stu(words char,explain char);";
	if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != 0)
	{
		fprintf(stderr,"%s\n",errmsg);
		return -1;
	}
	char choose;
	while(1)
	{
		system("clear");
		printf("------------------------\n");
		printf("---------1,增加---------\n");
		printf("---------2,删除---------\n");
		printf("---------3,修改---------\n");
		printf("---------4,查找---------\n");
		printf("---------5,退出---------\n");
		printf("------------------------\n");

		printf("请输入>>>>");
		choose=getchar();
		while(getchar()!=10);

		switch(choose)
		{
		case '1':do_insert(db,fp);
				 break;
		case '2':do_delete(db);
				 break;
		case '3':do_alter(db);
				 break;
		case '4':do_select(db);
				 break;
		case '5':goto END;
				 break;
		default:
				 printf("输入错误,请重新输入\n");
		}
		printf("请输入任何字符清屏>>>>");
		while(getchar()!=10);

	}
END:
	if(sqlite3_close(db) != 0)
	{
		fprintf(stderr,"%s\n",sqlite3_errmsg(db));
		return -1;
	}
	return 0;
}
void do_insert(sqlite3 *db,FILE *fp)
{	
	char *errmsg = NULL;
	char sql[256]="";
	char buf[128]="";
	char buf1[128]="";
	char buf2[128]="";
	int i=0;
	int j=0;
	while(1)
	{	
		bzero(buf,sizeof(buf));
		bzero(buf1,sizeof(buf1));
		bzero(buf2,sizeof(buf1));
		if(fgets(buf,sizeof(buf),fp) == NULL)
		{
			return;
		}
		buf[strlen(buf)-1]='\0';
		while(buf[i] != ' ')
		{
			buf1[i]=buf[i];
			i++;
		}
		while(buf[i] == ' ')
		{
			i++;
		}
		while(buf[i] != '\0')
		{
					buf2[j]=buf[i];
					i++;
					j++;
		}
		sprintf(sql,"insert into stu values(\"%s\",\"%s\");",buf1,buf2);
		if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != 0)
		{
			fprintf(stderr,"%s\n",errmsg);
			return;
		}
		i=0;j=0;

	}
}
void do_delete(sqlite3 *db)
{
	char *errmsg = NULL;
	char buf[128]="";
	char sql[256]="";	
	printf("请输入单词\n");
	scanf("%s",buf);
	while(getchar()!=10);
	sprintf(sql,"delete from stu where words='%s';",buf);
	if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != 0)
	{
		fprintf(stderr,"%s\n",errmsg);
		return;
	}
	printf("删除成功\n");
}
void do_alter(sqlite3 *db)
{
	char *errmsg = NULL;
	char words[128]="";
	char explain[128]="";
	char sql[256]="";
	printf("请输入要修改为什么词性和意思\n");
	scanf("%s",explain);	
	printf("请输入要修改的单词\n");
	scanf("%s",words);
	while(getchar()!=10);
	sprintf(sql,"update stu set explain='%s' where words='%s';",explain,words);
	if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != 0)
	{
		fprintf(stderr,"%s\n",errmsg);
		return;
	}
	printf("修改成功\n");

}
//查询相关的回调函数
int callback(void *arg,int ncolumns,char **column_text,char **column_name)
{
	if(0 == *((int *)arg))
	{
		for(int i=0;i<ncolumns;i++)
		{
			printf("%s\t",column_name[i]);
		}
		printf("\n");

		*((int *)arg) = 1;
	}
	for(int i;i<ncolumns;i++)
	{
		printf("%s\t",column_text[i]);
	}
	printf("\n");
	return 0;
}
void do_select(sqlite3 *db)
{
	char *errmsg = NULL;
	int flag=0;
	char sql[128]="";
	sprintf(sql,"select *from stu;");
	if(sqlite3_exec(db,sql,callback,&flag,&errmsg) != 0)
	{
		fprintf(stderr,"%s\n",errmsg);
		return;
	}
}

数据库模板

#include <stdio.h>
#include <sqlite3.h>
#include <stdlib.h>
void do_insert(sqlite3 *db);
void do_delete(sqlite3 *db);
void do_select(sqlite3 *db);
void do_alter(sqlite3 *db);
int main(int argc, const char *argv[])
{
	sqlite3 *db = NULL;
	int  fd=sqlite3_open("./my.db",&db);
	if(fd != 0)
	{
		printf("error\n");
		return -1;
	}
	printf("open success\n");
	//创建表格
	char *errmsg = NULL;
	char sql[128]="create table if not exists stu(id int,name char,score float);";
	if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != 0)
	{
		fprintf(stderr,"%s\n",errmsg);
		return -1;
	}
	char choose;
	while(1)
	{
		system("clear");
		printf("------------------------\n");
		printf("---------1,增加---------\n");
		printf("---------2,删除---------\n");
		printf("---------3,修改---------\n");
		printf("---------4,查找---------\n");
		printf("---------5,退出---------\n");
		printf("------------------------\n");

		printf("请输入>>>>");
		choose=getchar();
		while(getchar()!=10);

		switch(choose)
		{
		case '1':do_insert(db);
				 break;
		case '2':do_delete(db);
				 break;
		case '3':do_alter(db);
				 break;
		case '4':do_select(db);
				 break;
		case '5':goto END;
				 break;
		default:
				 printf("输入错误,请重新输入\n");
		}
		printf("请输入任何字符清屏>>>>");
		while(getchar()!=10);

	}
END:
	if(sqlite3_close(db) != 0)
	{
		fprintf(stderr,"%s\n",sqlite3_errmsg(db));
		return -1;
	}
	return 0;
}
void do_insert(sqlite3 *db)
{	
	char *errmsg = NULL;
	int id;
	char name[128];
	float score;
	printf("请输入id号\n");
	scanf("%d",&id);
	printf("请输入姓名\n");
	scanf("%s",name);
	printf("请输入分数\n");
	scanf("%f",&score);
	while(getchar()!=10);
	char sql[256]="";
	sprintf(sql,"insert into stu values(%d,'%s',%f);",id,name,score);
	if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != 0)
	{
		fprintf(stderr,"%s\n",errmsg);
		return;
	}
	printf("添加成功\n");
}
void do_delete(sqlite3 *db)
{
	char *errmsg = NULL;
	int id;
	char sql[256]="";	
	printf("请输入id号\n");
	scanf("%d",&id);
	while(getchar()!=10);
	sprintf(sql,"delete from stu where id=%d;",id);
	if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != 0)
	{
		fprintf(stderr,"%s\n",errmsg);
		return;
	}
	printf("删除成功\n");
}
void do_alter(sqlite3 *db)
{
	char *errmsg = NULL;
	int id;
	float score;
	char sql[256]="";
	printf("请输入要修改为多少分\n");
	scanf("%f",&score);	
	printf("请输入要修改的id号\n");
	scanf("%d",&id);
	while(getchar()!=10);
	sprintf(sql,"update stu set score=%f where id=%d;",score,id);
	if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != 0)
	{
		fprintf(stderr,"%s\n",errmsg);
		return;
	}
	printf("修改成功\n");

}
//查询相关的回调函数
int callback(void *arg,int ncolumns,char **column_text,char **column_name)
{
	if(0 == *((int *)arg))
	{
		for(int i=0;i<ncolumns;i++)
		{
			printf("%s\t",column_name[i]);
		}
		printf("\n");

		*((int *)arg) = 1;
	}
	for(int i;i<ncolumns;i++)
	{
		printf("%s\t",column_text[i]);
	}
	printf("\n");
	return 0;
}
void do_select(sqlite3 *db)
{
	char *errmsg = NULL;
	int flag=0;
	char sql[128]="";
	sprintf(sql,"select *from stu;");
	if(sqlite3_exec(db,sql,callback,&flag,&errmsg) != 0)
	{
		fprintf(stderr,"%s\n",errmsg);
		return;
	}
}

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/m0_71907123/article/details/129674681

网络编程-爱代码爱编程

网络编程 网络编程一、什么是网络二、计算机网络三、网络编程四、网络模型(OSI模型)五、TCP/IP模型六、TCP\UDP协议七、IP八、Port端口九、InetAddress类十、网络编程10.1 Socket编程