//*************************************************************************************** // C++ Insert/Update data into a Mysql database //*************************************************************************************** #include #include //Database Driver using namespace std; //Eliminate the need to type std:: (eg. std::string) int main() { try { sql::Driver *driver; sql::Connection *conn; sql::Statement *stmt; const string conn_str = "tcp://localhost/demo2"; //conn_string host:port/dbname const string user = "demo2"; //username const string pswd = "demo2"; //password driver = sql::mariadb::get_driver_instance(); //access DB driver conn = driver->connect(conn_str, user, pswd); //connect to DB stmt = conn->createStatement(); //create a statement string fname,lname,addr,flavor,top,cc; //create strings cout << "Enter first name: "; //prompt to enter cin >> fname; //read input cout << "Enter last name: "; cin >> lname; cout << "Enter address: "; cin >> addr; cout << "Enter ice cream flavor: "; cin >> flavor; cout << "Enter icec cream topping: "; cin >> top; cout << "Enter credit card: "; cin >> cc; string sql = "INSERT INTO cust_order "; sql += "VALUES(0,'"+fname+"','"+lname+"','"+addr+"','"+flavor+"','"+top+"','"+cc+"',1)"; // cout << sql << endl; //for debug only stmt->executeQuery(sql); //execute SQL delete stmt; delete conn; } catch (sql::SQLException &e) { std::cerr << "#ERROR: SQLException in " << __FILE__; std::cerr << "(" << __FUNCTION__ << ") on line " << __LINE__ << std::endl; std::cerr << "#ERROR: " << e.what() << std::endl; std::cerr << "(MySQL error code: " << e.getErrorCode() << ","; std::cerr << " SQLState: " << e.getSQLState() << ")" << std::endl; } return EXIT_SUCCESS; }