#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; }以上内容为博主原创,如需转载,请注明出处,谢谢!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。