我要保存一个ListView中的数据到Access表中,数据有多行多列,保存时从ListView中取出数据,通过insert into 命令插入到数据库中,代码如下
RecSet.Open(AFX_DAO_USE_DEFAULT_TYPE,"SELECT * FROM LogTable",0);
CString strTitle[4];
for (int i=0;i<nRow;i++)
{
for (int j=0;j<nCol;j++)
{
m_ListCtrl.GetHeaderCtrl()->GetItem(j,&hdi);
strTitle[j] = hdi.pszText;
strText[j] = m_ListCtrl.GetItemText(i,j);
}
db.Execute("INSERT INTO LogTable (strTitle[0]) VALUES (strText[0])");
}
当执行到 db.Execute("INSERT INTO LogTable (strTitle[0]) VALUES (strText[0])");时提示:"Syntax error in INSERT INTO statement",本人不太懂数据库,请问上面的insert into 语句错在哪里

解决方案 »

  1.   

    字段名(fileds)要写完整,比如
     INSERT INTO Employees " _
          & "(FirstName,LastName, Title) VALUES " _
          & "('Harry', 'Washington', 'Trainee');"
          
      

  2.   

    "INSERT INTO LogTable (strTitle[0]) VALUES (strText[0])"这种SQL语句当然不能执行了你得自己用程序构造一条可执行的SQL语句
      

  3.   

    那怎么将ListView中取出的值不用变量赋到values中呢,如果用变量那又该怎么办呢
      

  4.   

    for (int i = 0; i < nRow; i++)
    {
    if(nCol == 0)
    {
    continue;
    }

    CString strSql = _T("INSERT INTO LogTable (");

    CString strFields = _T("");
    CString strValues = _T("");
    for (int j = 0; j < nCol; j++)
    {
    if( j > 0 )
    {
    strFields += _T(",");
    strValues += _T(",");
    } m_ListCtrl.GetHeaderCtrl()->GetItem(j, &hdi);
    strFields += hdi.pszText;
    strValues += m_ListCtrl.GetItemText(i, j);
    } strSql += strFields + _T(") VALUES(") + strValues +_T(")"); db.Execute(strSql);
    }