Uploaded image for project: 'MariaDB Connector/C'
  1. MariaDB Connector/C
  2. CONC-699

UPDATE WHERE int mysql_stmt_affected_rows = 0

Details

    • Bug
    • Status: Open (View Workflow)
    • Major
    • Resolution: Unresolved
    • 3.3.8
    • None
    • Prepared Statements
    • None
    • Windows 10 Pro

    Description

      The following code fails to find the relevant row with WHERE id = ? condition.
      Server version: 10.11.8-MariaDB mariadb.org binary distribution

              MYSQL mysql;
       
              mysql_init(&mysql);
              mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP, "voicelog");
              if (!mysql_real_connect(&mysql,"localhost","voicelog","voicelog","voicelog",0,NULL,0))
              {
                  WriteToLog("Invalid database connection");
                  sleep(5);
                  continue;
              }
       
                      // return results to user
                      std::string results = output_str(ctx, pcmf32s);
       
                      MYSQL_STMT* gUpdateStmt = mysql_stmt_init(&mysql);
                      if (gUpdateStmt == NULL) {
                          WriteToLog("Init stmt failed");
                          continue;
                      }
       
                      std::cout << "Initialized statement\n";
       
                      char statement[] = "UPDATE voicelog SET transcription = ? WHERE id = ?";
                      int result = mysql_stmt_prepare(gUpdateStmt, statement, strlen(statement));  
                      if (result != 0) {
                          WriteToLog("prepare stmt failed");
                          continue;
                      }
       
                      std::cout << "Prepared statement\n";
       
                      /* Get the parameter count from the statement */
                      int param_count = mysql_stmt_param_count(gUpdateStmt);
                      std::cout << "Total parameters in UPDATE: " << param_count << "\n";
       
                      MYSQL_BIND bind[2];
       
                      memset(bind, 0, sizeof(bind));
       
                      bind[0].buffer_type = MYSQL_TYPE_STRING;
                      bind[0].buffer = (char *) results.c_str();
                      bind[0].is_null = 0;
                      unsigned long resultLength = results.length();
                      bind[0].length = &resultLength;
                      bind[0].buffer_length = resultLength + 1;
                      bind[1].buffer_type = MYSQL_TYPE_LONG;
                      const int id = atoi(row[1]);
                      bind[1].buffer = (char *) &id;
                      bind[1].is_null = 0;
                      bind[1].length = 0;
       
                      result = mysql_stmt_bind_param(gUpdateStmt, bind);
                      if (result != 0) {
                          WriteToLog("bind stmt bind failed");
                          continue;
                      }
                      
                      std::cout << "Bound '" << results << "', " << id << "\n";
       
                      result = mysql_stmt_execute(gUpdateStmt); 
                      if (result != 0) {
                          WriteToLog("execute stmt update failed");
                          std::cerr << mysql_stmt_error(gUpdateStmt) << "\n";
                          continue;
                      }
       
                      int affected_rows = mysql_stmt_affected_rows(gUpdateStmt);
                      std::cout << "Executed " << affected_rows << "\n";
      

      Attachments

        Activity

          georg Georg Richter added a comment -

          If mysql_affected_rows returns 0, then it means that either no row was found, or no row was updated, since it had already the same value. Without table definition and data it is impossible to analyze the exact problem.

          georg Georg Richter added a comment - If mysql_affected_rows returns 0, then it means that either no row was found, or no row was updated, since it had already the same value. Without table definition and data it is impossible to analyze the exact problem.

          MariaDB [voicelog]> select * from voicelog;
          +----------------------------------+---------------+------+
          | filename                         | transcription | id   |
          +----------------------------------+---------------+------+
          | C:\Users\matti\Downloads\jfk.wav | filled        |    1 |
          +----------------------------------+---------------+------+
          1 row in set (0.000 sec)
           
          MariaDB [voicelog]> show create table voicelog;
          +----------+-----------------------------------------------------------------------------------------------
          ------------------------------------------------------------------------------------------------+
          | Table    | Create Table
                                                                                                          |
          +----------+-----------------------------------------------------------------------------------------------
          ------------------------------------------------------------------------------------------------+
          | voicelog | CREATE TABLE `voicelog` (
            `filename` text DEFAULT NULL,
            `transcription` text DEFAULT NULL,
            `id` int(11) DEFAULT NULL
          ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci |
          +----------+-----------------------------------------------------------------------------------------------
          ------------------------------------------------------------------------------------------------+
          1 row in set (0.001 sec)
          
          

          mattilinnanvuori Matti Linnanvuori added a comment - MariaDB [voicelog]> select * from voicelog; +----------------------------------+---------------+------+ | filename | transcription | id | +----------------------------------+---------------+------+ | C:\Users\matti\Downloads\jfk.wav | filled | 1 | +----------------------------------+---------------+------+ 1 row in set (0.000 sec)   MariaDB [voicelog]> show create table voicelog; +----------+----------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------+ | Table | Create Table | +----------+----------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------+ | voicelog | CREATE TABLE `voicelog` ( `filename` text DEFAULT NULL, `transcription` text DEFAULT NULL, `id` int(11) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci | +----------+----------------------------------------------------------------------------------------------- ------------------------------------------------------------------------------------------------+ 1 row in set (0.001 sec)

                  MYSQL mysql;
           
                  mysql_init(&mysql);
                  mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP, "voicelog");
                  if (!mysql_real_connect(&mysql,"localhost","voicelog","voicelog","voicelog",0,NULL,0))
                  {
                      WriteToLog("Invalid database connection");
                      sleep(5);
                      continue;
                  }
           
                  /* Loop */
                  while(1)
                  {
                      if (mysql_query(&mysql, "SELECT filename, id FROM voicelog WHERE transcription IS NULL AND filename IS NOT NULL"))
                      {
                          WriteToLog("Failed to select filename");
                          sleep(5);
                          continue;
                      }
           
                      MYSQL_RES *result = mysql_store_result(&mysql);
           
                      if (result == NULL)
                      {
                          WriteToLog("Failed to store result");
                          sleep(5);
                          continue;
                      }
           
                      bool transcribed = false;
           
                      std::cout << mysql_num_fields(result) << " fields\n";
           
                      MYSQL_ROW row;
           
                      while ((row = mysql_fetch_row(result)))
                      {
                          std::string filename = row[0];
          
          

          mattilinnanvuori Matti Linnanvuori added a comment - MYSQL mysql;   mysql_init(&mysql); mysql_options(&mysql,MYSQL_READ_DEFAULT_GROUP, "voicelog"); if (!mysql_real_connect(&mysql,"localhost","voicelog","voicelog","voicelog",0,NULL,0)) { WriteToLog("Invalid database connection"); sleep(5); continue; }   /* Loop */ while(1) { if (mysql_query(&mysql, "SELECT filename, id FROM voicelog WHERE transcription IS NULL AND filename IS NOT NULL")) { WriteToLog("Failed to select filename"); sleep(5); continue; }   MYSQL_RES *result = mysql_store_result(&mysql);   if (result == NULL) { WriteToLog("Failed to store result"); sleep(5); continue; }   bool transcribed = false;   std::cout << mysql_num_fields(result) << " fields\n";   MYSQL_ROW row;   while ((row = mysql_fetch_row(result))) { std::string filename = row[0];

          The error happens on MSYS2 g++ compilation but not on Visual Studio.

          mattilinnanvuori Matti Linnanvuori added a comment - The error happens on MSYS2 g++ compilation but not on Visual Studio.

          People

            georg Georg Richter
            mattilinnanvuori Matti Linnanvuori
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:

              Git Integration

                Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.