You were doing two sqlite3_step()'s instead of one. So you skipped the one record you had inserted.
You can run this multiple times and you;; see error messages about the create table on 2nd and subsequent runs. Still adds the same record though so you'll see +1 records get printed out for every time you run it. Here's a complete working example: #include <iostream> #include "sqlite3.h" using namespace std; // TEST BENCH // 1. Create/Open a database 'db' // 2. Add a table in the database 'friend' // 3. add data to table friend // 4. Extract the data from table friend and display it sqlite3 *db; void OpenDB() { int rc; string dbName = "arbol.db"; rc = sqlite3_open_v2(dbName.c_str(), &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL); if(rc != SQLITE_OK) { /* display error msg */ cerr << "Error???" << endl; } } void CreateTable() { string create_table = "CREATE TABLE friend (name TEXT, address TEXT, age INT)"; sqlite3_stmt *stmt; int rc = sqlite3_prepare_v2( db, create_table.c_str(), -1, &stmt, NULL ); if(rc != SQLITE_OK) { cerr << "Error1: " << sqlite3_errmsg(db) << endl; } rc = sqlite3_step(stmt); if(rc != SQLITE_DONE) { cerr << "Error2: " << sqlite3_errmsg(db) << endl; } sqlite3_finalize(stmt); } void Setter() { string Name; sqlite3_stmt *stmt; string dbdata = "INSERT INTO friend (name, address, age) VALUES ('Caramba', '490 New Bridge', '49')"; int rc = sqlite3_prepare_v2(db, dbdata.c_str(),dbdata.length(),&stmt,NULL); if(rc != SQLITE_OK) { cerr << "Error: " << sqlite3_errmsg(db) << endl; } rc = sqlite3_step(stmt); if(rc != SQLITE_DONE) { cerr << "Error: " << sqlite3_errmsg(db) << endl; } sqlite3_finalize(stmt); } void getter() { string sName; string dbdata = "SELECT * FROM friend"; sqlite3_stmt *stmt; int rc = sqlite3_prepare_v2(db, dbdata.c_str(), -1, &stmt, NULL ); if(rc != SQLITE_OK) { cerr << "Error: " << sqlite3_errmsg(db) << endl; } while (sqlite3_step(stmt) == SQLITE_ROW) { sName = (char*)sqlite3_column_text(stmt, 0); //obj.Display(sName); //<== this is not display cout << "sName=" << sName << endl; } sqlite3_finalize(stmt); } void theDestructor() { int rc=sqlite3_close(db); if (rc != SQLITE_OK) { cerr << "Error: " << sqlite3_errmsg(db) << endl; } } int main() { OpenDB(); CreateTable(); Setter(); getter(); theDestructor(); return 0; } Michael D. Black Senior Scientist Advanced Analytics Directorate Advanced GEOINT Solutions Operating Unit Northrop Grumman Information Systems ________________________________ From: sqlite-users-boun...@sqlite.org [sqlite-users-boun...@sqlite.org] on behalf of Arbol One [arbol...@gmail.com] Sent: Monday, June 25, 2012 9:30 AM To: 'General Discussion of SQLite Database' Subject: EXT :Re: [sqlite] C++ programming - Extracting data I did what you suggested, you were right, now the exception is gone, but the program now does not display anything, it just goes to the end of the method. Here is a more complete snip of the test bench program. I really hope that someone here can help resolve this issue. // TEST BENCH // 1. Create/Open a database 'db' // 2. Add a table in the database 'friend' // 3. add data to table friend // 4. Extract the data from table friend and display it OpenDB() { rc = sqlite3_open_v2(dbName.c_str(), &db, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL); if(rc != SQLITE_OK) { /* display error msg */ } .... } CreateTable() { create_table = "CREATE TABLE friend (name TEXT, address TEXT, age INT)"; rc = sqlite3_prepare_v2( db, create_table.c_str(), -1, &stmt, NULL ); if(rc != SQLITE_OK) { /* display error msg */ } rc = sqlite3_step(stmt); if(rc != SQLITE_DONE) { /* display error msg */ } sqlite3_finalize(stmt); } Setter() { string Name; string dbdata = "INSERT INTO friend (name, address, age) VALUES ('Caramba', '490 New Bridge', '49')"; rc = sqlite3_prepare_v2(db, bdata.c_str(),dbdata.length(),&stmt,NULL); if(rc != SQLITE_OK) { /* display error msg */ } ..... rc = sqlite3_step(stmt); if(rc != SQLITE_DONE) { /* display error msg */} sqlite3_finalize(stmt); } getter() { string sName; string dbdata = "SELECT * FROM friend"; rc = sqlite3_prepare_v2(db, dbdata.c_str(), -1, &stmt, NULL ); if(rc != SQLITE_OK) { /* display error msg */ } .... rc = sqlite3_step(stmt); if(rc != (SQLITE_DONE) && rc != (SQLITE_ROW)){ ... } while (sqlite3_step(stmt) == SQLITE_ROW) { sName = (char*)sqlite3_column_text(stmt, 0); obj.Display(sName); //<== this is not display ... } sqlite3_finalize(stmt); } theDestructor() { sqlite3_close(db); } -----Original Message----- From: sqlite-users-boun...@sqlite.org [mailto:sqlite-users-boun...@sqlite.org] On Behalf Of Black, Michael (IS) Sent: Monday, June 25, 2012 9:39 AM To: General Discussion of SQLite Database Subject: Re: [sqlite] C++ programming - Extracting data You're not doing the right sequencing...so your cout is only executing when there is NOT a row. Change rc = sqlite3_step(stmt); if(rc != SQLITE_DONE) { ... } while ( sqlite3_step(stmt) != SQLITE_ROW) { sName = (char*)sqlite3_column_text(stmt, 0); //<== Seg fault std::cout << sName << std::endl; ... } To while ( sqlite3_step(stmt) == SQLITE_ROW) { sName = (char*)sqlite3_column_text(stmt, 0); //<== Seg fault std::cout << sName << std::endl; ... } Michael D. Black Senior Scientist Advanced Analytics Directorate Advanced GEOINT Solutions Operating Unit Northrop Grumman Information Systems ________________________________ From: sqlite-users-boun...@sqlite.org [sqlite-users-boun...@sqlite.org] on behalf of Arbol One [arbol...@gmail.com] Sent: Monday, June 25, 2012 8:27 AM To: 'General Discussion of SQLite Database' Subject: EXT :Re: [sqlite] C++ programming - Extracting data Sorry, I forgot to give you a snip of the test-bench code getter() { string sName; string sAddress; int age = 0; string dbdata = "SELECT * FROM friend"; rc = sqlite3_prepare_v2(db, dbdata.c_str(), -1, &stmt, NULL ); .... rc = sqlite3_step(stmt); if(rc != SQLITE_DONE) { ... } while ( sqlite3_step(stmt) != SQLITE_ROW) { sName = (char*)sqlite3_column_text(stmt, 0); //<== Seg fault std::cout << sName << std::endl; ... } sqlite3_finalize(stmt); } -----Original Message----- From: sqlite-users-boun...@sqlite.org [mailto:sqlite-users-boun...@sqlite.org] On Behalf Of Igor Tandetnik Sent: Monday, June 25, 2012 9:07 AM To: sqlite-users@sqlite.org Subject: Re: [sqlite] C++ programming - creating a table Arbol One <arbol...@gmail.com> wrote: > Q: What are you trying to achieve here? What's the supposed purpose of > sqlite3_column_type call? > > A: What I am trying to do is to display the data in a data table You run a SELECT statement for that. In any case, what data do you expect there to be right after CREATE TABLE? -- Igor Tandetnik _______________________________________________ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users _______________________________________________ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users _______________________________________________ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users _______________________________________________ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users _______________________________________________ sqlite-users mailing list sqlite-users@sqlite.org http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users