0

I'm new to SQLite, C++ and having some trouble understanding how one deals with multiple rows returned from a query. I need to put the value of each column int a variable so I can deal with it later.

I have so far:

sqlite3_stmt* statement;

if (sqlite3_prepare_v2(getDatabase(), _sql.c_str(), -1, &statement, 0)
            == SQLITE_OK)
{
    int cols = sqlite3_column_count(statement);
    int result = 0;

    while(true)
    {
        result = sqlite3_step(statement);

        if(result == SQLITE_ROW)
        {
             for(int col = 0; col < cols; col++)
             {
                // go through each column and store it       
             }
        }
        else
        {
            break;
        }
    }
}

so what do I do in my for loop?

if (col == 0) // first column position
{
   // store it...
}
else if (col == 1) // second column position
{
    // store it...
}

or is there a way to do it by column name?

or is there a better way to go about this?

1
  • What's the end goal? After the loop is complete, where exactly do you want the data to end up, and what do you want to do with it next? Re: column names - see sqlite3_column_name Apr 20, 2014 at 0:56

1 Answer 1

0

I figured it out.

The correct procedure is to loop through each row and use sqlite_column(statement, [col#])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.