mardi 26 novembre 2019

terminate called after throwing an instance of 'std::__cxx11::basic_string

I tried to write a sample project to use sqlite. Everytime I enter the ID i get the following error message: terminate called after throwing an instance of 'std::__cxx11::basic_string, std::allocator >'

Can anyone please help me?

Header Database.h

...
private:
void showMenu();
void addRecord();
void showRecord();
void openDB();

int nSelection;

sqlite3* db;
int rc;
std::string sql;

const unsigned char* uc_result_firstName;
const unsigned char* uc_result_lastName;

int nRowID;
std::string strFirstName;
std::string strLastName;
int nAge;

}; ...

cpp File Database.cpp

...
void Database::showRecord(){

std::cout << "Select ID -> ";
int nID;
std::string strBuffer = "";
std::cin >> strBuffer;
nID = std::stoi(strBuffer.c_str());

sqlite3_stmt* stmt;
rc = sqlite3_prepare_v2(db, "SELECT FirstName, LastName, Age"
                                " FROM customerTable"
                                " WHERE id = ?", -1, &stmt, NULL);

if (rc != SQLITE_OK)
    throw std::string(sqlite3_errmsg(db));

rc = sqlite3_bind_int(stmt, 1, nID);            // Using parameters ("?") is not
if (rc != SQLITE_OK) {                          // really necessary, but recommended
    std::string errmsg(sqlite3_errmsg(db));     // (especially for strings) to avoid
    sqlite3_finalize(stmt);                     // formatting problems and SQL
    throw errmsg;                               // injection attacks.
}

rc = sqlite3_step(stmt);
if (rc != SQLITE_ROW && rc != SQLITE_DONE) {
    std::string errmsg(sqlite3_errmsg(db));
    sqlite3_finalize(stmt);
    throw errmsg;
}
if (rc == SQLITE_DONE) {
    sqlite3_finalize(stmt);
    throw std::string("customer not found");
}

uc_result_firstName = sqlite3_column_text(stmt, 0);
uc_result_lastName = sqlite3_column_text(stmt, 1);

this->nRowID     = nID;
this->strFirstName = std::string(reinterpret_cast<char const*>(uc_result_firstName));
this->strLastName  = std::string(reinterpret_cast<char const*>(uc_result_lastName));
this->nAge         = sqlite3_column_int(stmt, 2);

sqlite3_finalize(stmt);

std::cout << "\nLastname: " << strLastName << std::endl;

}

Aucun commentaire:

Enregistrer un commentaire