Rokata
Rokata

Reputation: 1229

Error when executing query with SQLite in C++

Okay so i have 2 methods in my code: one to create a database and one to insert records. Here they are:

void DatabaseManager::insert_entry_into_database() {
ostringstream ss;
int choice;

do {
    cout << "Press 1 to add a teacher or 2 for student";
    cin >> choice;
} while (choice != 1 && choice != 2);       

if (choice == 1) {
    Teacher t;
    ss << "INSERT INTO Teachers ('" << t.get_firstName() << "', '" << t.get_lastName() << "', " << t.get_NID() 
        << ", " << t.get_avg_horarium() << ");";
}

const string tmp = ss.str();
const char *query = tmp.c_str();

cout << query;

sqlite3_open("Database.sqlite", &database);
if (sqlite3_prepare_v2(database, query, -1, &statement, 0) == SQLITE_OK) {
    cout << "A new record has been added to the database!" << endl;
}
else {
    cout << "Error adding new record!";
}   
sqlite3_finalize(statement);
sqlite3_close(database); 
}

void DatabaseManager::create_database() {
sqlite3_open("Database.sqlite", &database);

char * query = "CREATE TABLE Students (firstName STRING, lastName STRING, NID BIGINT, grade DOUBLE);";

if (sqlite3_prepare_v2(database, query, -1, &statement, 0) == SQLITE_OK) {
    cout << "Empty database with students created!" << endl;
}

query = "CREATE TABLE Teachers (firstName STRING, lastName STRING, NID BIGINT, avg_horarium INT);";

if (sqlite3_prepare_v2(database, query, -1, &statement, 0) == SQLITE_OK) {
    cout << "Empty database with teachers created!" << endl;
}

sqlite3_finalize(statement);
sqlite3_close(database);

}

I test them in the main function:

int _tmain(int argc, _TCHAR* argv[])
{
DatabaseManager m;
m.create_database();
m.insert_entry_into_database();
return 0;
}

The create table queries seem to be succesful and the confirmation message shows on the console but the insert query is treated as invalid. I've added a cout << query call to make sure it's correct and it is a hundred percent valid. What's wrong with this?


The select method

vector<vector<string>> DatabaseManager::select_entry() {
vector<vector<string>> results;
sqlite3_open("Database.sqlite", &database);

char* query = "SELECT firstName FROM Teachers WHERE firstName = 'Ivan';";
cout << query;

if (sqlite3_prepare_v2(database, query, -1, &statement, 0) == SQLITE_OK) {
    int cols = sqlite3_column_count(statement);
    int result = 0;

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

        if (result == SQLITE_ROW) {
            cout << "test"; 
            vector<string> values;
            for (int i=0; i<cols; i++) {
                cout << (char*)sqlite3_column_text(statement, i);
                values.push_back((char*)sqlite3_column_text(statement, i));
            }
            results.push_back(values);
        }
        else break;
    }

    sqlite3_finalize(statement);
    string error = sqlite3_errmsg(database);
    if(error != "not an error") cout << query << " " << error << endl;

    return results;
}

}

Upvotes: 0

Views: 1681

Answers (1)

CL.
CL.

Reputation: 180040

The valid syntax for an INSERT statement is either

INSERT INTO MyTable(Col1, Col2, ...) VALUES (value1, value2, ...)

or

INSERT INTO MyTable VALUES (value1, value2, ...)

You have omitted VALUES, so SQLite thinks that the stuff inside the parentheses is the list of column names.


The database file is created automatically by sqlite3_open.


You need to call sqlite3_step to actually execute your commands.

Upvotes: 1

Related Questions