我必须向数据库添加数据。我在我的vc ++应用程序中使用MySQL Connector。 我按照以下方法创建并向该表中的某些列插入值。
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");
取决于按钮单击,我将不同的值添加到同一个表中的不同列。
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查询失败。当我检查Temp String时,在字符串的末尾它有额外的字符为0.00(null)。
Temp =" INSERT INTO perfscoretbl(SessionID,TagName,Description,TagLow,TagHigh,ExtremeLow,ExtremeHigh,AvgDev,Score)VALUES(' Ins1_03122015_115222T',' DPC101& #39;,' DEPROP PR' 16.00',' 18.00',' 11.26',' 11.26& #39;,' 100.00'' 0.00&#39)的 0.00(空)"
我检查了一切,但无法纠正。 在vc ++ mysql连接器中是否有另一种INSERT查询方式?
答案 0 :(得分:3)
看起来最后%0.2f%s
是不受欢迎的。
答案 1 :(得分:2)
JijeshKV's anwser的补充:
我将逗号和撇号放入格式字符串中,这导致格式函数的参数少得多,并且更具可读性,如下所示:
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,"')"
);