Tell me more ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

We are running a C program where for every one second there is a function callback. Below is the snippet of the code listed. It is ran over every second without missing so we notice sometimes when is busy doing the insert part and the next select is up and running and cause this error. How to overcome this problem we have tried to copy the main localRes1 into another localRes2 and then try to free it but also got into errors.

char timeBuf[10],secondBuf1[100],queryBuf1[500],queryBuf2[500];
char buff[20] = {0};
struct timeval tv;
gettimeofday (&tv, NULL);
tv.tv_sec -= 5;
strftime(buff, 20, "%Y-%m-%d %H:%M:%S", localtime(&tv.tv_sec));
printf("\nTime is %s", buff);

sprintf(secondBuf1,"INSERT INTO  secondsLog2 (secondLogID , timeStampID ) VALUES (NULL,'%s')",buff);
//printf("Query 1 before executing %s\n",queryBuf1);
if (mysql_query(localConn, secondBuf1)) 
{
    printf("Error in insert of seconds log %s\n",mysql_error(localConn));
    exit(1);
}

sprintf(queryBuf1,"SELECT ipDest, portDest, SUM(packetLen), COUNT(ipDest) FROM source1 WHERE timeStamp = '%s' GROUP BY portDest",buff);
printf("\nQuery buf %s",queryBuf1);
if(mysql_query(remoteConn, queryBuf1))
{
    printf("Error in first query of select %s\n",mysql_error(remoteConn));
    exit(1);
}
localRes1 = mysql_use_result(remoteConn);
while((localRow1 = mysql_fetch_row(localRes1)) !=NULL)
            {
              sprintf(queryBuf1,"INSERT INTO  export1 (iBTID ,timeStampID ,ipDest ,portDest,totalBits, packetCount) VALUES (NULL,'%s','%s','%s',%s,%s)",buff, localRow1[0],localRow1[1],localRow1[2],localRow1[3],localRow1[4]);
              printf("Query 1 before executing %s\n",queryBuf1);
              if (mysql_query(localConn, queryBuf1)) 
              {
                printf("Error in first query of insert %s\n",mysql_error(localConn));
                 exit(1);
              }

            }
                        mysql_free_result(localRes1);
share|improve this question
It's more of a programming question. Consider to post at stackoverflow site. However, one solution is to wait until the insert is complete. So you will need to use threads to sync read and writes. – Ashwin kumar Jul 17 '12 at 7:22
I suspect OP is question banned on SO, since the migration failed. – JNK Jul 17 '12 at 12:56

closed as off topic by JNK Jul 17 '12 at 12:55

Questions on Database Administrators Stack Exchange are expected to relate to database administration within the scope defined in the FAQ. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about closed questions here.

Browse other questions tagged or ask your own question.