-1

I have this simple code:

    std::stringstream del;
    int ret = 0;
    del<<"DELETE FROM 'database_name' WHERE id=\""<<id<<"\""; //id is a parameter of the function
    std::string query = del.str();
    const char* ch_q = query.c_str();
    ret = mysql_query(conn, ch_q);
    if(ret == 0)
    {
        std::cout<<"Record deleted successfully"<<std::endl;
        return true;
    }else{
        std::cout<<"Failed to delete record"<<std::endl;
        return false;
    }

The id row in the database has a type of TEXT and is a PRIMARY KEY Every time I run the code, it fails to delete the element.

Shadow
  • 33,525
  • 10
  • 51
  • 64
Batyu
  • 19
  • 2

1 Answers1

0

Your statement has an error where you're naming the table.

From the MySQL manual page on identifiers:

The identifier quote character is the backtick (`)

So:

DELETE FROM `database_name`

You've used a single-quote instead:

DELETE FROM 'database_name'
Asteroids With Wings
  • 17,071
  • 2
  • 21
  • 35