在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;   
}
 
 以上内容为博主原创,如需转载,请注明出处,谢谢! 

相关文章

ubuntu退出redis的示例:指定配置文件方式启动源码redis:roo...
ubuntu中mysql改密码忘了的解决方法:1.在终端中切换到root权...
ubuntu安装mysql失败的解决方法原因:可能是原有的MySQL还有...
使用centos和ubuntu建站的区别有以下几点1.CentOS是Linux发行...
ubuntu图形界面和字符界面切换的方法:可以通过快捷键CTRL+A...
ubuntu中重启mysql失败的解决方法1.首先,在ubuntu命令行中,...