2023/09/04 网络编程

#include 
//插入
int do_insert(sqlite3 *db)
{
	//打开文件dict.txt
	FILE *fd;
	if((fd = fopen("dict.txt","r"))==NULL)
	{
		ERR_MSG("fopen");
	}
	printf("fopen success\n");

	char buf[256]= "";
	char *E= NULL;
	char *C= NULL;
	char sql1[256]="";
	char *errmsg=NULL;
	int i=0;
	while(fgets(buf,sizeof(buf),fd)!=NULL)
	{
		buf[strlen(buf)-1]='\0';
		//分割中英文
		E=strtok(buf," ");
		C=strtok(NULL,"\0");
		//将英文和中文插入到表中
		sprintf(sql1,"insert into word values('%s','%s');",E,C);
		if(sqlite3_exec(db,sql1,NULL,NULL,&errmsg)!=SQLITE_OK)
		{
			printf("exec:%d __%d__\n",sqlite3_errcode(db),__LINE__);
			return -1;
		}
		i++;
		printf("%d\n",i);
	}
	printf("插入完成\n");
	fclose(fd);
	return 0;
}


int main(int argc, const char *argv[])
{
	//打开数据库
	sqlite3 *db;
	if(sqlite3_open("./dict.db",&db)!=SQLITE_OK)
	{
		printf("open :%s %d __%d__\n",sqlite3_errmsg(db),sqlite3_errcode(db),__LINE__);
		return -1;
	}
	printf("open success\n");
	//创建单词表
	char *errmsg;
	char sql[128] = "create table if not exists word(name char, mean char);";
	if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
	{
		printf("exec:%d __%d__\n",sqlite3_errcode(db),__LINE__);
		return -1;
	}
	printf("create table success\n");
	//调用插入函数
	do_insert(db);
	//关闭数据库	
	if(sqlite3_close(db)!=SQLITE_OK)
	{
		printf("close :%s %d __%d__\n",sqlite3_errmsg(db),sqlite3_errcode(db),__LINE__);
		return -1;	
	}
	printf("close success\n");


	return 0;
}

你可能感兴趣的:(网络,linux,ubuntu,c语言)