multiple mysql_real_query() in while loop
Posted
by Steve
on Stack Overflow
See other posts from Stack Overflow
or by Steve
Published on 2008-11-11T09:26:59Z
Indexed on
2010/04/06
6:53 UTC
Read the original article
Hit count: 149
It seems that when I have one mysql_real_query() function in a continuous while loop, the query will get executed OK.
However, if multiple mysql_real_query() are inside the while loop, one right after the other. Depending on the query, sometimes neither the first query nor second query will execute properly.
This seems like a threading issue to me. I'm wondering if the mysql c api has a way of dealing with this? Does anyone know how to deal with this? mysql_free_result() doesn't work since I am not even storing 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);
}
}
© Stack Overflow or respective owner