mercredi 25 février 2015

C++ sqlite- Error- what(): basic_string::_S_construct null not valid

I have a sqlite table SiteCode having following schema:



CREATE TABLE SiteCode(
ID INTEGER PRIMARY KEY,
Code TEXT(3) UNIQUE NOT NULL DEFAULT 0);


The values in SiteCode are as follows:



ID Code
---------- ----------
7 789
8 78
9 785
10 998


Following is the header file to access it using C++


DBAccess1.h



#ifndef LIB_DBAccess1_HH
#define LIB_DBAccess1_HH
using namespace std;

class sqliteDB {

private:
int rc;
char *zErrMsg;
sqlite3 *db;
const char *sql;
sqlite3_stmt * stmt5;
list<string> Site_Code_list;
public:
//=================================XX====================================XX====================================//
//=====SITE-CODE=====//
//=====GET FUNCTION=====//
list<string> GET_Site_Code(const char* CodeB)
{
int CodeA;

list<string> Site_Code_list;

int rc = sqlite3_open("/DBsqlite3/empdbv3.db", &db);

if (rc != SQLITE_OK) {
cerr << "Cannot open database [ " << sqlite3_errmsg(db) << " ]" << endl;
sqlite3_close(db);
}

sql = "SELECT Code FROM SiteCode WHERE ID= @ID;";

rc = sqlite3_prepare_v2(db, sql, -1, &stmt5, 0);

if(SQLITE_OK != rc) {
cerr << "Failed to PREPARE SELECT statement for SiteCode Table [ " << sqlite3_errmsg(db) << " ]" << endl;
sqlite3_finalize(stmt5);
sqlite3_close(db);
exit(1);
}
else {
int Code_x = sqlite3_bind_parameter_index(stmt5, "@ID");

char* buffer1=const_cast<char*>(CodeB); //converting const char* to char*

cout<<"Buffer1="<<buffer1<<endl;
CodeA=atoi(buffer1); //Converting char* to int
cout<<"CodeA="<<CodeA<<endl;

if(isdigit(CodeA)!= 0) {
cout<<"\nPlease enter only digits\n"<<endl;
}
else {
if((CodeA >= 000) && (CodeA <= 998)) {
sqlite3_bind_int(stmt5, Code_x, CodeA);
cout<<"Binding done successfully"<<endl;
}
else {
cout<< "Valid ID Should be between 000 to 998\n" <<endl;
}
}//else loop of (isdigit) ends here
}//else loop of (cin.fail) ends here


int column = sqlite3_column_count(stmt5);

for(int i = 0; i < column; i++)
{
cout << "Columns=" << column << endl;
Site_Code_list.push_back(string((const char *) sqlite3_column_text(stmt5, i))); // ORIGIN OF ERROR LINE-AS PER ME
cout<<"Pushed back in List"<<endl;
}

if(sqlite3_step(stmt5)==SQLITE_DONE) {
cout<<"\nRecord fetching completed"<< endl;
}
else {
cout<<"\nERROR while fetching record from the SiteCode table[ " << sqlite3_errmsg(db) << " ]" << endl;
}

sqlite3_finalize(stmt5);
sqlite3_close(db);

return Site_Code_list;
}

//=================================XX====================================XX====================================//


The main file is main.cpp



#include <iostream>
#include <sqlite3.h>
#include <stdlib.h>
#include <list>
#include <iterator>
#include <algorithm>
#include <cstring>
#include <unistd.h>
#include <string>
#include <cstdio>
#include <sstream>
#include <cctype>
#include <cstdlib>

#include "DBAccess1.h"
//#include "Check_Individual_Function1.h"

using namespace std ;

int main()
{

sqliteDB object1;

const char* value1 = "10";

object1.GET_Site_Code(value1);

cout << "\n\nAll the statement were executed properly\n\n";

return 0;
}


when i execute the program i get the following results with error:



Buffer1=10
CodeA=10
Binding done successfully
Columns=1
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_S_construct null not valid
Aborted (core dumped)


As per my understanding, I believe the error is because I am returning a NULL string from the function GET_Site_Code. So I conclude that no data is getting pushed into the LIST- Site_Code_list.


As per my understanding, the line Site_Code_list.push_back(string((const char *) sqlite3_column_text(stmt5, i))); is the problem.


How do i solve it or how do I approach? Kindly Help.


Aucun commentaire:

Enregistrer un commentaire