急问,数据库查询出错。
数据库条件查询,代码如下:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int i;
AnsiString str = "";
if(Edit1->Text == "")
{
ShowMessage("请输入编号");
return;
}
else
{
str = Edit1->Text;
ShowMessage(Edit1->Text);
Query1->Active = false;
Query1->SQL->Clear();
Query1->SQL->Add("select * from employee.db where FirstName =" + str);
Query1->ExecSQL();
Query1->Active = true;
}
}
运行后,例如输入信息Kim给str令其查找FirstName为Kim的数据,提示错误信息为invalid field name Kim
请问这个错误该如何解决呢?谢谢
[解决办法]
Query1->SQL->Add("select * from employee.db where FirstName =" + str);
改成Query1->SQL->Add("select * from employee.db where FirstName =" + QuotedStr(str));