Search code examples
c++mysql++

mysql++ query fails


I'm a new user of mysql++ looking for some pointers (pun intended).

The problem: my update statement fails.

The connection is open. The previous statement that uses the connection works.

I'm sure the record I'm trying to update exists. I can see it with the mysql query tools. I'm sure CustomerId is correct.

     // declaration of the customer id
     uint32_t CustomerId;

Why does this fail to update?

   mysqlpp::Connection conn( true );
   try
   {
      if ( conn.connect( db_rw.Name, db_rw.Host, db_rw.User, db_rw.Password ) )
      {

         // *snip*  insert code here works fine.

         // this query fails
         mysqlpp::Query query = conn.query( "UPDATE customer SET AccountName=%2q, Active=%3, Password=%1 WHERE CustomerId=%0" );
         query.parse();
         mysqlpp::SQLQueryParms parms;
         parms.push_back( mysqlpp::sql_int( CustomerId ) );
         parms.push_back( mysqlpp::sql_blob( data, sizeof(data) ) ); //<- 16 byte binary blob
         parms.push_back( mysqlpp::sql_varchar( widget.AccountName->text().toAscii().data() ) );  // string
         parms.push_back( mysqlpp::sql_bool( widget.ActiveCheckBox->checkState() == Qt::Checked ? 1 : 0 ) );  // 
         mysqlpp::SimpleResult res = query.execute( parms );
      }
   }

If I turn off exceptions for the connection it fails silently (the result.info() method returns nothing).

if I turn exceptions on it seg faults when trying to convert to a string:

std::string Query::str(SQLQueryParms& p)
{
    if (!parse_elems_.empty()) {
        proc(p);
    }

    return sbuffer_.str();
}

Solution

  • There were several issues.

    The library doesn't escape binary data correctly.

    If the user associated with the connection does not have update permission then the library will crash instead of throwing an exception.