简体   繁体   中英

.txt to SQlite3 with C++

I want to read data from txt file and insert it into SQlite database table with C++. I prepared a code but it doesn't work.

Example line from my txt file is follows; ',1417392060.000000','1.245430','1.2456','1.24469','1.245000'

And code;

  sqlite3 *db; char *zErrMsgt = 0; int rct; int rch; int rchi; rct = sqlite3_open("final.db", &db); if( rct ){ fprintf(stderr, "Can't open database: %s\\n", sqlite3_errmsg(db)); exit(0); }else{ fprintf(stdout, "Opened database successfully\\n"); } string resline; ifstream res("Res.txt"); while(getline(res,resline)){ string sqlli = "INSERT into FOREXNEW(DATE,OPEN,HIGH,LOW,CLOSE) VALUES ("getline(res,resline)");"; rchi = sqlite3_exec(db, sqlli.c_str(), callback, 0, &zErrMsgt); if( rchi != SQLITE_OK ){ cout << "SQL error:" << zErrMsgt; sqlite3_free(zErrMsgt); }} sqlite3_close(db); 

Any alternate suggestions and solutions will be appreciated as well:) Thank you.

Getline doesn't return a string; it copies it to the variable passed in the second parameter. Change the erroneous line:

   string sqlli = "INSERT into FOREXNEW(DATE,OPEN,HIGH,LOW,CLOSE) VALUES ("getline(res,resline)");";

to:

   string sqlli = "INSERT into FOREXNEW(DATE,OPEN,HIGH,LOW,CLOSE) VALUES ("+resline+");";

And check if it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM