将文件中的内容存储到数据库里面 sqlite3

        使用前需要在Ubuntu下载sqlite3数据库

1.确保虚拟机能联网
​    2.更新更新源
​        sudo apt-get update

​    3、安装软件及开发环境
        sudo apt-get install sqlite3            --->sqlite3数据库软件
        sudo apt-get install libsqlite3-dev            --->sqlite3数据库开发支持库
        sudo apt-get install sqlitebrowser        --->sqlite3数据库操作软件

安装成功后,终端输入以下指令,判断是否安装成功:
    linux@linux:~$ sqlite3

出现下列语句,标识安装成功
    SQLite version 3.7.2     版本无所谓
    Enter ".help" for instructions
    Enter SQL statements terminated with a ";"
    sqlite> 

输入    .quit     退出数据库
    linux@linux:~$ sqlite3
    SQLite version 3.7.2
    Enter ".help" for instructions
    Enter SQL statements terminated with a ";"
    sqlite> .quit
    linux@linux:~$ 
 

这里是将一个词典录入数据库:

#include <stdio.h> 
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sqlite3.h>

#define ERR_MSG(msg) {\
	fprintf(stderr,"line: %d\n",__LINE__);\
	perror(msg);\
}

int main(int argc, const char *argv[])
{
	sqlite3 *db = NULL;
	if(sqlite3_open("./my.db", &db) < 0){
		fprintf(stderr,"sqlite3_open failed %d | %s\n",\
				sqlite3_errcode(db), sqlite3_errmsg(db));
		return -1;
	}
	printf("create sqlite3 success\n");

	//操作数据库
	char sql[128] = "create table if not exists cidian (id int primary key, word char, wordType char);";
	char *errmsg = NULL;
	if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK){
		fprintf(stderr,"sqlite3_exec failed %s\n", errmsg);
		return -1;
	}
	printf("table stu create success __%d__\n", __LINE__);
	
	int fd = open("./dict.txt",O_RDONLY);
	if(fd < 0){
		ERR_MSG("open");
		return -1;
	}
	
	ssize_t size = 0;
	char buf[2] = "\0";
	char dataLine[128] = "\0";
	int num = 1;
	while(1){
		bzero(dataLine, sizeof(dataLine));
		int n = 0;
		while(1){
			bzero(buf,sizeof(buf));
			size = read(fd, buf, 1);
			if(0 == size){
				break;
			}
			else if(size < 0){
				ERR_MSG("read");
			}
			if(buf[0] == '\n')
				break;
			dataLine[n++] = buf[0];
		}
		if(size == 0){
			break;
		}
		char word[128] = "\0";
		char wordType[128] = "\0";
		int type2 = 0;
		int n2 = 0;
		int type3 = 0;
		for(int i = 0; i < strlen(dataLine); i ++){
			if(dataLine[i] == ' ' && type3 == 0){
				type2 = 1;
				n2 = 0;
				continue;
			}
			if(type2 == 0){
				word[n2++] = dataLine[i];
			}
			if(type2 == 1){
				type3 = 1;
				wordType[n2++] = dataLine[i];
			}
		}
		char sql[128] = "\0";
		sprintf(sql, "insert into cidian values(%d, \"%s\", \"%s\");",num++, word, wordType);
		char *errmsg = NULL;
		if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK){
			fprintf(stderr,"sqlite3_exec failed %s\n", errmsg);
			return -1;
		}
	}

	if(sqlite3_close(db) != SQLITE_OK){
		fprintf(stderr,"sqlite3_close failed %d | %s\n",\
				sqlite3_errcode(db), sqlite3_errmsg(db));
		return -1;
	}
	printf("close sqlite3 success\n");
	return 0;
}

结果如下:

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值