Your logic is incorrect. You have already stepped to the first row before you
enter the loop, which starts out by stepping again, thus exhausting the
returned data (since you only have one row). If you insert multiple rows your
code may work but leave out the first row.
Try something like:
getter()
{
string sName;
string dbdata = "SELECT * FROM friend";
rc = sqlite3_prepare_v2(db, dbdata.c_str(), -1, &stmt, NULL );
if (rc == SQLITE_OK)
{
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW)
{
sName = (char*)sqlite3_column_text(stmt, 0);
obj.Display(sName); //<== this is not display
...
}
}
sqlite3_finalize(stmt);
return (rc == SQLITE_DONE) ? SQLITE_OK : rc
}
---
() ascii ribbon campaign against html e-mail
/\ www.asciiribbon.org
> -----Original Message-----
> From: [email protected] [mailto:sqlite-users-
> [email protected]] On Behalf Of Arbol One
> Sent: Monday, 25 June, 2012 08:31
> To: General Discussion of SQLite Database
> Subject: 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: [email protected]
> [mailto:[email protected]] 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: [email protected] [[email protected]] on
> behalf of Arbol One [[email protected]]
> 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: [email protected]
> [mailto:[email protected]] On Behalf Of Igor Tandetnik
> Sent: Monday, June 25, 2012 9:07 AM
> To: [email protected]
> Subject: Re: [sqlite] C++ programming - creating a table
>
> Arbol One <[email protected]> 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
> [email protected]
> http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
>
> _______________________________________________
> sqlite-users mailing list
> [email protected]
> http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
> _______________________________________________
> sqlite-users mailing list
> [email protected]
> http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
>
> _______________________________________________
> sqlite-users mailing list
> [email protected]
> http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users
_______________________________________________
sqlite-users mailing list
[email protected]
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users