SQL语句:INSERT INTO 200712 ( SN, Fail, [Count], FirstFailFilePath )
VALUES ('07N543711860', '1', '1', 'D:\Project\AvayaReportFilter\Source\FtsRes_SPICE-BT-3_07N543711860_20071029210801570_200710291381867.xml');在Access2003中运行没有问题。其中红色的字段都是C++变量,包括TCHAR tableName[260]; TCHAR sN[260]; TCHAR firstFailFilePath[260];
我用了两种方法构造这个语句,方法1: /* _tcscat(insertCommand, tableName);
_tcscat(insertCommand, _T(" (SN,Fail,Count,FirstFailFilePath)"));
_tcscat(insertCommand, _T(" VALUES "));
_tcscat(insertCommand, _T("('"));
_tcscat(insertCommand, _T(serialNumber));
_tcscat(insertCommand, _T("','1','1','"));
_tcscat(insertCommand, _T(strFileName));
_tcscat(insertCommand, _T("')"));方法2:
TCHAR insertCommand[MAX_PATH] = "INSERT INTO :tableName (SN, Fail, Count, FirstFailFilePath) VALUES (':serialNumber','1','1',':strFileName',)";都不能查询,debug第一种方法得到的字符串是可以在手动在数据库执行的。
VALUES ('07N543711860', '1', '1', 'D:\Project\AvayaReportFilter\Source\FtsRes_SPICE-BT-3_07N543711860_20071029210801570_200710291381867.xml');在Access2003中运行没有问题。其中红色的字段都是C++变量,包括TCHAR tableName[260]; TCHAR sN[260]; TCHAR firstFailFilePath[260];
我用了两种方法构造这个语句,方法1: /* _tcscat(insertCommand, tableName);
_tcscat(insertCommand, _T(" (SN,Fail,Count,FirstFailFilePath)"));
_tcscat(insertCommand, _T(" VALUES "));
_tcscat(insertCommand, _T("('"));
_tcscat(insertCommand, _T(serialNumber));
_tcscat(insertCommand, _T("','1','1','"));
_tcscat(insertCommand, _T(strFileName));
_tcscat(insertCommand, _T("')"));方法2:
TCHAR insertCommand[MAX_PATH] = "INSERT INTO :tableName (SN, Fail, Count, FirstFailFilePath) VALUES (':serialNumber','1','1',':strFileName',)";都不能查询,debug第一种方法得到的字符串是可以在手动在数据库执行的。
VALUES ('07N543711860', '1', '1', 'D:\Project\AvayaReportFilter\Source\FtsRes_SPICE-BT-3_07N543711860_20071029210801570_200710291381867.xml');
sql.format(INSERT INTO %s(SN, Fail, Count, FirstFailFilePath) VALUES ('%s','1','1','%s'),tablename,:serialNumber,strFileName)
sql.Format("INSERT INTO %s(SN,Fail,Count,FirstFailFilePath) VALUES ('%s','1','1','%s')",tableName,serialNumber,strFileName);
cmd->SetText(sql);
rs->Execute(cmd);仍然是INSERT INTO syntax error
sql.Format("INSERT INTO %s (SN,Fail,[Count],FirstFailFilePath) VALUES ('%s','1','1','%s')",tableName,serialNumber,strFileName);
cmd->SetText(sql);
rs->Execute(cmd);
前面没注意...
他会给你一个改良后的结果
比如这里,如果在access中查旬count没有加方括号
查询依然正确,而且再次打开盖查询时,会发现他自动给你加了方括号
第一种方法不说了,这里说第二种方法:TCHAR insertCommand[MAX_PATH] = "INSERT INTO :tableName (SN, Fail, Count, FirstFailFilePath) VALUES (':serialNumber','1','1',':strFileName',)";这条语句的意思:1、你声明了一个 TCHAR 类型的数组 insertCommand
2、数组大小为 MAX_PATH
3、并赋值为一个字符串,是用双引号括起来的,如:"xxx"说明:你在这个字符串里放入了所谓的变量,但是在C语言中,这样只是一个字符串,它并不会把 :serialNumber 等替换成实际的内容;所以程序运行后,向数据库实际上就发送了上面的字符串,所以数据库不能识别。你需要用上边“我是菜鸟”的方法,生成正确的 SQL 语句。由于我不知道这个表中各列的数据类型,所以我这里只举个很简单的例子,供你参考:CString strSQL;
// 下面是学号,整型
int nStuNo = 10;
// 下面是姓名,字符串
CString strName("Tom");// 生成INSERT语句
strSQL.Format( "INSERT INTO student(stu_no,stu_name) VALUES( %d , '%s' )" , nStuNo , strName);// 用 MessageBox 显示一下生成的SQL语句
MessageBox(strSQL);注意:%s 前后有一对单引号,这是很重要的。这里只是一个简单的例子,楼主还需要认真研究!
这句话,不全对,要不要单引号是根据 DBMS 决定的;有些DBMS用的不是单引号,也许是其它什么符号。就是说,这个VC++没关系,跟DBMS有关系。