I am working on a beginner level C++ banking system project using MySQL data base server. In order to use a database from the server I have used Connect/C++ library from MySQL website. The main file was compiling and no linking errors, but as soon as the program executes a function which executes sql query, there is an error. I have given one of the functions code which shows the following error statement in console “Unknown column ‘ ‘ in ‘field list’ (MySQL error code: 1054, SQLState: 42S22) using MySQL connect/C++”. I have used try-catch block in main() function.
void createCust(sql::Connection* con)
{
std::string name;
std::cout<<"Enter your first name: ";
std::cin >> name;
std::string rand = std::to_string((std::rand() % 10000)); //generated a random number to assign acc_no to new customer
this->account.acc_no = accIsPresent(con, rand); // assign unique account numbers
// Create a statement
std::unique_ptr<sql::Statement> stmt(con->createStatement());
// query1: create a row with the first_name taken as input
std::ostringstream query1, query2, query3, query4;
query1 << "INSERT INTO customer (first_name) VALUES ("
<< name << ")";
// Execute query1
stmt->executeUpdate(query1.str());
// query2: fetch 'id' from table customer
query2 << "SELECT id FROM customer WHERE first_name = ' " << name <<" ' ";
sql::ResultSet* res = stmt->executeQuery(query2.str());
// assign customer id to cust_id
this->cust_id = res->getInt("id");
// add account for the new customer
query3 << "INSERT INTO accounts(cust_id, acc_no)" << "VALUES (" << this->cust_id <<", " << this->account.acc_no <<")"; // error will show
stmt->executeUpdate(query3.str());
query4 << "UPDATE accounts SET bal = 0, transaction0 = 0, transaction1= 0, transaction2 = 0 WHERE acc_no = '" << this->account.acc_no << "'";
stmt->executeUpdate(query4.str());
this->account.balance = 0;
std::cout << "Account created Successfully!" << std::endl;
std::cout << "Your Customer ID is " << this->cust_id << " and your Account Number is " << this->account.acc_no << std::endl;
}
I tried to check the spelling and syntax. I also re-wrote the function, but the same error message was shown. I think I am implementing the library wrong? I am trying to insert a new row when a customer is created (new customer) and then fetch his “cust_id” to store it locally for executing other sql queries.