day 6

//用c语言实现对sqlite3数据库的插入删除修改和查找

//头文件
#ifndef __HEAD__
#define __HEAD__
#include <quick.h>
#define sqname "my.db"
#define tabname "stu"
void Sq_Insert(sqlite3 *db);
void Sq_Delete(sqlite3 *db);
void Sq_Update(sqlite3 *db);
void Sq_Search(sqlite3 *db);
#endif
//主函数
#include "head.h"
int main(int argc, const char *argv[])
{
	//打开数据库
	sqlite3 *db;
	if(sqlite3_open(sqname,&db)!=0)
	{
		fprintf(stdout,"sqlite3_open : [%d] : %s\n",sqlite3_errcode(db),sqlite3_errmsg(db));
		return -1;
	}
	printf("my.db open success\n");
	//变量
	char comnum = 0;
	//创建表格
	char *command = "create table if not exists stu(id int primary key,name char,score float);";
	char *errmsg;
	if(sqlite3_exec(db,command,NULL,NULL,&errmsg)!=0)
	{
		fprintf(stdout,"create table : %s\n",errmsg);
		return -1;
	}
	printf("table create success\n");
	//任务选择
	while(1)
	{
		printf("-----1 insert-----\n");
		printf("-----2 delete-----\n");
		printf("-----3 update-----\n");
		printf("-----4 search-----\n");
		printf("-----5  quit -----\n");
		//终端输入任务码
		printf("please enter command num : ");
		comnum = getchar();
		while(getchar()!=10);
		//选择入口
		switch(comnum)
		{
		case '1': //插入
			Sq_Insert(db);
			break;
		case '2': //删除
			Sq_Delete(db);
			break;
		case '3': //修改
			Sq_Update(db);
			break;
		case '4': //查找
			Sq_Search(db);
			break;
		case '5': //退出
			goto END;
			break;
		default :
			printf("format error,please enter again\n");
			break;
		}
	}

END:
	//关闭数据库
	if(sqlite3_close(db)!=0)
	{	
		fprintf(stdout,"sqlite3_close : [%d] : %s\n",sqlite3_errcode(db),sqlite3_errmsg(db));
		return -1;
	}
	return 0;
}
//子函数
#include "head.h"
//插入
void Sq_Insert(sqlite3 *db)
{
	//从终端获得插入的数据
	printf("id:  name:  score:\n");
	printf("please enter one set of data :");
	int id;
	char name[20];
	float score;
	scanf("%d %s %f",&id,name,&score);
	while(getchar()!=10);
	//将数据组合成数据库命令
	char command[128] = "";
	sprintf(command,"insert into %s values (%d,\"%s\",%f)",tabname,id,name,score);
	//写入数据库
	char *errmsg;
	if(sqlite3_exec(db,command,NULL,NULL,&errmsg)!=0)
	{
		fprintf(stdout,"insert into : %s\n",errmsg);
		return ;	
	}
	printf("insert into success\n");
	return ;
}
//删除
void Sq_Delete(sqlite3 *db)
{
	//获取删除条件
	printf("please enter delete limit : ");
	char limit[20] = "";
	scanf("%s",limit);
	while(getchar()!=10);
	//将数据组合成数据库命令
	char command[128] = "";
	sprintf(command,"delete from %s where %s",tabname,limit);
	//写入数据库
	char *errmsg;
	if(sqlite3_exec(db,command,NULL,NULL,&errmsg)!=0)
	{
		fprintf(stdout,"delete : %s\n",errmsg);
		return ;	
	}
	printf("delete success\n");
	return ;
}
//修改
void Sq_Update(sqlite3 *db)
{
	//获取修改条件
	printf("please enter update limit : ");
	char limit[20] = "";
	scanf("%s",limit);
	while(getchar()!=10);
	//获取修改数据
	printf("please enter update data : (eg:score = 100)");
	char update[20] = "";
	scanf("%s",update);
	while(getchar()!=10);
	//将数据组合成数据库命令
	char command[128] = "";
	sprintf(command,"update %s set %s where %s",tabname,update,limit);
	//写入数据库
	char *errmsg;
	if(sqlite3_exec(db,command,NULL,NULL,&errmsg)!=0)
	{
		fprintf(stdout,"update : %s\n",errmsg);
		return ;	
	}
	printf("update success\n");
	return ;
}
//查询
void Sq_Search(sqlite3 *db)
{
	printf("buildding\n");
	return;
}

将词典带编号导入到数据库中

#include <quick.h>
#define sqname "dict.db"
#define tabname "dict"
int main(int argc, const char *argv[])
{
	//打开词典.txt
	int dfd = open("./dict.txt",O_RDONLY);
	if(dfd < 0)
	{
		perror("open");
		return -1;
	}
	printf("dict.txt is opened\n");
	//打开词典.db
	sqlite3 *db;
	if(sqlite3_open(sqname,&db) != 0)
	{
		fprintf(stdout,"sqlite3_open [%d] : %s\n",sqlite3_errcode(db),sqlite3_errmsg(db));
		return -1;
	}
	printf("dict.db is opened\n");
	//创建一个表
	char command[128] = "";
	char *errmsg = NULL;
	sprintf(command,"create table if not exists %s (seq int primary key,word char,mean char)",tabname);
	if(sqlite3_exec(db,command,NULL,NULL,&errmsg) != 0)
	{
		printf("table create : %s\n",errmsg);
		return -1;
	}
	printf("table is created\n");
	//将词典单词写入数据库
	char buf;
	char word[20] = "";
	char mean[50] = "";
	int seq = 0;
	int i = 0;
	ssize_t res = 0;
	//从文件夹中读取单词、意思
	while(1)
	{	
		seq++;
		bzero(word,sizeof(word));
		bzero(mean,sizeof(mean));
		i=0;
		//写入word
		while(1)
		{
			res = read(dfd,&buf,1);
			//判断是否写入结束
			if(res == 0)
			{
				printf("insert into finished\n");
				goto END;
			}
			else if(buf == ' ')
				break;
			else
			{
				word[i]=buf;
				i++;
			}
		}
		i=0;
		//写入mean
		while(1)
		{
			read(dfd,&buf,1);
			if(buf == ' ')
				continue;
			else if(buf == '\n')
				break;
			else
			{
				mean[i]=buf;
				i++;
			}
		}
		//一个单词信息读取完毕
		//组合数据库命令
		sprintf(command,"insert into %s values (%d,\"%s\",\"%s\")",tabname,seq,word,mean);
		//将数据写入数据库
		if(sqlite3_exec(db,command,NULL,NULL,&errmsg) != 0)
		{
			printf("insert into : %s\n",errmsg);
			return -1;	
		}
		printf("[%d] insert into success\n",seq);
	}
END:
	//关闭文件
	close(dfd);
	//关闭数据库
	if(sqlite3_close(db) != 0)
	{
		fprintf(stdout,"sqlite3_open [%d] : %s\n",sqlite3_errcode(db),sqlite3_errmsg(db));
		return -1;	
	}
	return 0;
}

C:\Users\admin\Desktop\前端开发\Node.js\day6\code\api_server\node_modules\mysql\lib\protocol\Parser.js:437 throw err; // Rethrow non-MySQL errors ^ Error: secretOrPrivateKey must have a value at module.exports [as sign] (C:\Users\admin\Desktop\前端开发\Node.js\day6\code\api_server\node_modules\jsonwebtoken\sign.js:107:20) at Query.<anonymous> (C:\Users\admin\Desktop\前端开发\Node.js\day6\code\api_server\router_handler\2user.js:49:26) at Query.<anonymous> (C:\Users\admin\Desktop\前端开发\Node.js\day6\code\api_server\node_modules\mysql\lib\Connection.js:526:10) at Query._callback (C:\Users\admin\Desktop\前端开发\Node.js\day6\code\api_server\node_modules\mysql\lib\Connection.js:488:16) at Sequence.end (C:\Users\admin\Desktop\前端开发\Node.js\day6\code\api_server\node_modules\mysql\lib\protocol\sequences\Sequence.js:83:24) at Query._handleFinalResultPacket (C:\Users\admin\Desktop\前端开发\Node.js\day6\code\api_server\node_modules\mysql\lib\protocol\sequences\Query.js:149:8) at Query.EofPacket (C:\Users\admin\Desktop\前端开发\Node.js\day6\code\api_server\node_modules\mysql\lib\protocol\sequences\Query.js:133:8) at Protocol._parsePacket (C:\Users\admin\Desktop\前端开发\Node.js\day6\code\api_server\node_modules\mysql\lib\protocol\Protocol.js:291:23) at Parser._parsePacket (C:\Users\admin\Desktop\前端开发\Node.js\day6\code\api_server\node_modules\mysql\lib\protocol\Parser.js:433:10) at Parser.write (C:\Users\admin\Desktop\前端开发\Node.js\day6\code\api_server\node_modules\mysql\lib\protocol\Parser.js:43:10) Node.js v18.12.1
06-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值