Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8864991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:23:34+00:00 2026-06-14T16:23:34+00:00

Okay so i have 2 methods in my code: one to create a database

  • 0

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;
}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T16:23:35+00:00Added an answer on June 14, 2026 at 4:23 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay. I have this code on my site: <?php session_start(); include database.php; include bruger.php;
Okay I have updated my code quite a bit. I am getting a new
Okay I have updated my code a little, but I am still not exactly
Okay I have created an application where in one of the screens I have
I have two methods that do the same thing. The first one makes performance
Okay, here's the situation. I have two NSManagedObjects called Store and Aisle . There
I have code similar to following: void processObjectRecursively(Object obj) { Field[] fields = obj.getClass().getDeclaredFields();
Okay I have spent the last 2 days trying to sort this one out.
Okay so I have the following Code which appends a string to another in
Okay, so I want to have a generic method that can read data from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.