Table is read Only

sun_flower 2009-05-04 04:40:31
此代码的功能:对数据库进行数据的写入,首先判断数据库中有没有要插入的sn(sn是主键),如果有就执行update语句,如果没有就新插入这个sn记录。但是我用以下代码执行的时候update语句可以执行,为什么到了insert语句的时候就报错,提示: cannot perform this operation on closed dataset;请各位赐教!谢谢!
Form2->Query1->Close() ;
for(int i=0; i <nbP; i++)
{
Form2->Query1->RequestLive = true ;
ShowMessage("start");
Form2->Query1->SQL->Clear() ;
Form2->Query1->SQL->Add("select * from label_demo ;") ;
Form2->Query1->Open() ;
Form2->Query1->First() ;
int p_qty ;
while(!Form2->Query1->Eof)
{
ShowMessage("while start");
Form2->Query1->Edit() ;
if(Form2->Query1->FieldByName("sn")->AsInteger == snLong)
{
p_qty = Form2->Query1->FieldByName("p_qty")->AsInteger + 1 ;
ShowMessage("start update");
Form2->Query1->SQL->Clear() ;
Form2->Query1->SQL->Add("update label_demo set p_qty = :p_qty where sn = :sn ;") ;
Form2->Query1->ParamByName("p_qty")->AsInteger = p_qty ;
Form2->Query1->ParamByName("sn")->AsInteger = snLong ;
Form2->Query1->ExecSQL() ;
ShowMessage(" find it ");
}
Form2->Query1->Next() ;
}
//Form2->Query1->Open() ;
Form2->Query1->SQL->Clear() ;
Form2->Query1->SQL->Add("insert into label_demo (sn,pc,mac1,mac2) values (:sn,:pc,:mac1,:mac2)") ;
Form2->Query1->Params->ParamByName("sn")->AsInteger = snLong ;
Form2->Query1->Params->ParamByName("pc")->AsString = Form2->pcVar->Text.c_str() ;
Form2->Query1->Params->ParamByName("mac1")->AsInteger = mac1Long ;
Form2->Query1->Params->ParamByName("mac2")->AsInteger = mac2Long ;
Form2->Query1->ExecSQL() ;
snLong += 1 ;
mac1Long += 8 ;
mac2Long += 8 ;
}
...全文
124 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
fairchild811 2009-05-04
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zhouzhangkui 的回复:]
modi :
if(Form2->Query1->FindFirst())
[/Quote]

up
周药师 2009-05-04
  • 打赏
  • 举报
回复
modi :
if(Form2->Query1->FindFirst())
周药师 2009-05-04
  • 打赏
  • 举报
回复
應該加一個判斷
if(Form2->Query1->First())

將後面代碼放入
else
{
//Form2->Query1->Open() ;
Form2->Query1->SQL->Clear() ;
Form2->Query1->SQL->Add("insert into label_demo (sn,pc,mac1,mac2) values (:sn,:pc,:mac1,:mac2)") ;
Form2->Query1->Params->ParamByName("sn")->AsInteger = snLong ;
Form2->Query1->Params->ParamByName("pc")->AsString = Form2->pcVar->Text.c_str() ;
Form2->Query1->Params->ParamByName("mac1")->AsInteger = mac1Long ;
Form2->Query1->Params->ParamByName("mac2")->AsInteger = mac2Long ;
Form2->Query1->ExecSQL() ;
snLong += 1 ;
mac1Long += 8 ;
mac2Long += 8 ;

}

否則你的這段代碼永遠會執行,
那麼之後插入這個主鍵的時候時候 跟你修改后的主鍵相同
當然添加不進去!

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧