Search code examples
c++sqlmysql-error-1064

INSERT INTO SQL With c++ error 1064 42000


I am facing the error code while i want to insert into the sql data base

error :1064 sqlstate :42000

Here is my code:

string* DB(string barcode)
{
    try {
        sql::Driver *driver;
        sql::Connection *con;
        sql::Statement *stmt;
        sql::ResultSet *res;
        /* Create a connection */
        driver = get_driver_instance();
        con = driver->connect("sql14.main-hosting.eu", "u103222348_user", "newsystem");
        /* Connect to the MySQL test database */
        con->setSchema("u103222348_read");

        stmt = con->createStatement();

        res = stmt->executeQuery("INSERT INTO 'Conveyor-BELT' ('BARCODE', 'TIM121') VALUES('5555','5555')");
        while (res->next()) {

            /* Access column data by alias or column name */


        }
        delete res;
        delete stmt;
        delete con;

    }
    catch (sql::SQLException &e) {
        cout << "# ERR: SQLException in " << __FILE__;
        cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
        cout << "# ERR: " << e.what();
        cout << " (MySQL error code: " << e.getErrorCode();
        cout << ", SQLState: " << e.getSQLState() << " )" << endl;
    }

    return EXIT_SUCCESS;
}

Solution

  • In MySQL use backticks to escape column and table names and quotes to escape strings

    INSERT INTO `Conveyor-BELT` (`BARCODE`, `TIM121`)
    VALUES('5555', '5555')