Multiple mysql_real_query () in while loop

It seems that when I have one mysql_real_query () function in a continuous while loop, the query will be executed in order.

However, if multiple mysql_real_query () loops are inside the while loop, one by one. Depending on the request, sometimes the first request or the second request is not executed.

It seems to me that this is a problem with threads. I am wondering if mysql c api method has deal with this? Does anyone know how to deal with this? mysql_free_result () does not work, since I do not even save the results.

//keep polling as long as stop character '-' is not read
while(szRxChar != '-')
{
    // Check if a read is outstanding
    if (HasOverlappedIoCompleted(&ovRead))
    {
        // Issue a serial port read
        if (!ReadFile(hSerial,&szRxChar,1,
                &dwBytesRead,&ovRead))
        {
            DWORD dwErr = GetLastError();
            if (dwErr!=ERROR_IO_PENDING)
                return dwErr;
        }
    }

    // Wait 5 seconds for serial input
    if (!(HasOverlappedIoCompleted(&ovRead)))
    {
        WaitForSingleObject(hReadEvent,RESET_TIME);
    }

    // Check if serial input has arrived
    if (GetOverlappedResult(hSerial,&ovRead,
            &dwBytesRead,FALSE))
    {
        // Wait for the write
        GetOverlappedResult(hSerial,&ovWrite,
            &dwBytesWritten,TRUE);

        //load tagBuffer with byte stream
        tagBuffer[i] = szRxChar;
        i++;
        tagBuffer[i] = 0; //char arrays are \0 terminated

        //run query with tagBuffer
        if( strlen(tagBuffer)==PACKET_LENGTH )
        {
            sprintf(query,"insert into scan (rfidnum) values ('");
            strcat(query, tagBuffer);
            strcat(query, "')");
            mysql_real_query(&mysql,query,(unsigned int)strlen(query));

            i=0;
        }

        mysql_real_query(&mysql,"insert into scan (rfidnum) values ('2nd query')",(unsigned int)strlen("insert into scan (rfid) values ('2nd query')"));

        mysql_free_result(res);
    }
}
+3
source share
2 answers

Always check the return value of the API call.

mysql_real_query()returns an integer. The value will be zero if the call worked, and nonzero if there is an error.

, :

if ((err = mysql_real_query(&mysql,"insert into scan (rfidnum) values ('2nd query')",
  (unsigned int)strlen("insert into scan (rfid) values ('2nd query')"))) != 0)
{
  // report err here, get additional information from these two API calls:
  errno = mysql_errno(&mysql);
  errmsg = mysql_error(&mysql);
}

update: , mysql_error(), , . , , - SELECT, , CR_COMMANDS_OUT_OF_SYNC, , API , ( ). SQL-, SELECT ( ), .

MySQL:

mysql_free_result(), . , mysql_use_result(), .

mysql_use_result() doc:

mysql_query() mysql_real_query(), mysql_store_result() mysql_use_result() set (SELECT, SHOW, DESCRIBE, EXPLAIN, CHECK TABLE ..). mysql_free_result() .

+1

. tagBuffer mysql_real_query , .

. .

, , tagBuffer. , - MySQL, , ?

0

Source: https://habr.com/ru/post/1699401/


All Articles