CString 格式为 MySQL 查询添加空值

CString Format adds null value for MySQL query

我必须向数据库添加数据。我在 vc++ 应用程序中使用 MySQL 连接器。 我正在按照以下方法创建值并将值插入 table.

中的某些列
 if(mysql_query(&dbSql , "CREATE TABLE IF NOT EXISTS perfscoretbl (SessionID CHAR(100) NOT NULL,TagName CHAR(50) NOT NULL ,\
     Description CHAR(50) NOT NULL,TagLow CHAR(50) NOT NULL,TagHigh CHAR(50) NOT NULL, ExtremeLow CHAR(50) NOT NULL,ExtremeHigh CHAR(50) NOT NULL,\
     AvgDev CHAR(50)  NOT NULL,PVHighTime CHAR(50) NOT NULL,PVLowTime CHAR(50) NOT NULL,PV CHAR(50) NOT NULL,\
     TotalTime CHAR(50) NOT NULL,FinalDeviation CHAR(50) NOT NULL ,Score CHAR(50) NOT NULL) ")==0) 

         printf( "Table Created\n");

取决于按钮的点击我将不同的值添加到同一列中的不同列table。

CString Temp,sTmp;
sTmp="INSERT INTO perfscoretbl (SessionID, TagName, Description,TagLow,TagHigh,ExtremeLow,ExtremeHigh,AvgDev,Score) VALUES(";
Temp.Format("%s%s%s%s%s%s%s%s%0.2f%s%0.2f%s%0.2f%s%0.2f%s%0.2f%s%0.2f%s%0.2f%s",sTmp,"'",sessionId,"','",pPerfData->sTagName,"','", \
pPerfData->sDesc,"','",pPerfData->fTagLow,"','",pPerfData->fTagHigh,"','",pPerfData->fExtremeLow,"','",pPerfData->fExtremeHigh,"','", \
pPerfData->fError,"','",pPerfData->fTagScore,"')" );

if(mysql_query(&dbSql,Temp)==0)
printf("Succeed");

但是 INSERT 查询失败。当我检查临时字符串时,在字符串的末尾有额外的字符 0.00(null)。

温度 = "INSERT INTO perfscoretbl (SessionID, TagName, Description,TagLow,TagHigh,ExtremeLow,ExtremeHigh,AvgDev,Score) VALUES('Ins1_03122015_115222T','DPC101','DEPROP PR','16.00','18.00','11.26','11.26','100.00','0.00')0.00(null)"

我检查了所有内容,但无法更正。 vc++ mysql 连接器中的 INSERT 查询还有其他方法吗?

看起来最后一个 %0.2f%s 不需要。

JijeshKV 回答的补充:

我将逗号和撇号放入格式字符串中,这样 Format 函数的参数就少了很多,可读性也更高了,如下所示:

CString Temp,sTmp;
sTmp="INSERT INTO perfscoretbl (SessionID, TagName, Description, TagLow, TagHigh, ExtremeLow, ExtremeHigh, AvgDev, Score) VALUES(";
Temp.Format("'%s', '%s', '%s', '%s', '%0.2f', '%0.2f', '%0.2f', '%0.2f', '%0.2f', '%0.2f'",
             sTmp,
             sessionId,
             pPerfData->sTagName,
             pPerfData->sDesc,
             pPerfData->fTagLow,
             pPerfData->fTagHigh,
             pPerfData->fExtremeLow,
             pPerfData->fExtremeHigh,
             pPerfData->fError,
             pPerfData->fTagScore,"')"
           );