1

I am trying to convert 3 columns from an SQLite table to 3 arrays. I have the current code to get the DB:

sqlite3 *db; char *error;
int rc;
rc = sqlite3_open("db.sql", &db);
if (rc)
{
    cout << "Error Connecting";
    sqlite3_close(db);
}
else {
    char **results = NULL;
    int rows, columns;
    const char *sqlSelect = "SELECT name, surname, dob FROM data";
    sqlite3_get_table(db, sqlSelect, &results, &rows, &columns, &error);
    if (rc) {
        system("color 0c");
        cout << "Error executing query";
    }
    else {
    // What to put here?
    }
}

I am having trouble continuing, the documentation is a little hard for me to understand. How can I convert my array 'results' to something useful? (etc 3 arrays, dobs[] names[] surnames[])

1
  • fwiw; the get_table api is maintained for backwards compatibility, you should use newer methods. Commented Feb 12, 2016 at 21:31

1 Answer 1

1

The first 3 elements of results contain the field names. After that they are the records, one field at a time.

For example:

results[0] = "name";
results[1] = "surname";
results[2] = "dob";
results[3] = "John";
results[4] = "Doe";
results[5] = "01/01/1970";
// etc...

So you want to skip the first 3 results, and then put the remainder into containers. Don't use arrays. Put them in 3 vectors:

std::vector<std::string> names;
std::vector<std::string> surnames;
std::vector<std::string> dobs;

for (int i = 2; i < rows; i += 3) {
    names.push_back(results[i]);
    surnames.push_back(results[i + 1]);
    dobs.push_back(results[i + 2]);
}
Sign up to request clarification or add additional context in comments.

2 Comments

Namespace std has no member 'vector'?
You need to #include <vector> (and <string>.)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.