在ubuntu上使用sqlite3 对csv文件进行操作

前端之家收集整理的这篇文章主要介绍了在ubuntu上使用sqlite3 对csv文件进行操作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
static int callback(void *NotUsed,int argc,char **argv,char **azColName){
   int i;
   for(i=0; i < argc; i++){
      printf("%s = %s\n",azColName[i],argv[i] ? argv[i] : "NULL");
   }
   printf("\n");
   return 0;
}
union FloatRand
{
    struct 
    {
        unsigned long Frac:23;
        unsigned long Exp:8;
        unsigned long Signed:1;
    } BitArea;
    float Value;
};
float GetFloatRand()
{
    union FloatRand r;
    r.BitArea.Signed = 0;
    r.BitArea.Exp = 1;
    r.BitArea.Frac = (rand() * rand()) % 0x800000;

    if (r.BitArea.Frac == 0x7FFFFF)
        r.BitArea.Exp = rand() % 0x7E;
    else if (r.BitArea.Frac == 0)
        r.BitArea.Exp = rand() % 0x7E + 1;
    else
        r.BitArea.Exp = rand() % 0x7F;

    return r.Value;
}

class MysqLite3
{
public:
	MysqLite3(){};
	~MysqLite3(){};
public:
	int    open(const char* dbfile);
	int    created();
	string readcsvfile(const char* filename,int len);
	int    from_str_to_vector(std::string& sourceStr,std::vector<float>& vector);
	int    insertToDB_form_csv(const char* csvfile,int floatlen);
private:
	sqlite3 *db;
	char 	*zErrMsg = 0;
	const char* CREATE_TABLE = "create table feat(name VARCHAR(128),num INT,vectors CLOB);";
public:
	inline void   close(){
		sqlite3_close(db);
		std::cout << "sqlite3_close successfully" << std::endl;
	}
};

int MysqLite3::open(const char* dbfile)
{
	int rc = sqlite3_open(dbfile,&db);
	if(rc){
      fprintf(stderr,"Can't open database: %s\n",sqlite3_errmsg(db));
      exit(0);
    }
    else
    {
      fprintf(stderr,"Opened database successfully\n");
    }
    return  0;
}
int MysqLite3::created(){
	int rc = sqlite3_exec(db,CREATE_TABLE,callback,&zErrMsg);
   	if( rc != sqlITE_OK ){
   		fprintf(stderr,"sql error: %s\n",zErrMsg);
      	sqlite3_free(zErrMsg);
   	}
   	else
   	{
      	fprintf(stdout,"Table created successfully\n");
   	}
   	return 0; 
}
string MysqLite3::readcsvfile(const char* filename,int len)
{
	string out("");
	//std::cout << "str max: " << out.max_size() << std::endl;
	FILE *file = fopen(filename,"r");
	if( file != NULL)
	{
		int    i = 0;
		char   line[64]={0};
		while( (fgets(line,sizeof line,file) != NULL) && (i < len))
		{
			string tt_line = string(line);
			tt_line.erase(tt_line.length()-1,1);

			string t = string(tt_line) + string(",");
			out    += t;

			i++;
		}
		fclose(file);
	}else{
		std::cout << "open file Failed" << std::endl;
	}
	//std::cout << "str__ max: " << out << std::endl;
	return out;
}
int MysqLite3::from_str_to_vector(std::string& sourceStr,std::vector<float>& vector){
	 int     i = 0;
	 int     index  = 0;
	 int     length = 0;
	 char    ff[32]={0};
	
	 int     ok = 0;
	 while(i < sourceStr.length() && (i != std::string::npos)){
	 	if (sourceStr[i] == ',')
	 	{
	 		float t_f = atof(ff);
	 		vector.push_back(t_f);

	 		memset(ff,sizeof(ff)/sizeof(char));

	 		index = 0;
	 		ok ++;

	 	}else{
	 		ff[index] = sourceStr[i];
	 		index++;
	 		//std::cout << "data: "<< ff << std::endl;
	 	}
	 	i++;
	 }
	 std::cout  << "ok  num :" << ok << std::endl;
	 return 0;
}

int MysqLite3::insertToDB_form_csv(const char* csvfile,int floatlen)
{
	string  out = readcsvfile(csvfile,floatlen);
	char    index[2048000]={0};

	sprintf(index,"insert into feat (name,num,vectors) values ('are',%d,'%s');",1,out.c_str());
   	int rc = sqlite3_exec(db,index,zErrMsg);
      	sqlite3_free(zErrMsg);
   	}
   	else
   	{
       fprintf(stdout,"insertToDB_form_csv successfully\n");
   	} 
   	return 0;
}
int main(int argc,char* argv[])
{
   	MysqLite3 sq;
   	sq.open("face.db");
   	sq.created();
   	sq.close();
	return 0;   
}
 
 以上内容为博主原创,如需转载,请注明出处,谢谢! 
原文链接:https://www.f2er.com/ubuntu/349427.html

猜你在找的Ubuntu相关文章