1

我正在尝试使用 Delphi 7 中的 SQL 查询在数据库中创建记录。我正在使用 ADO 查询,我尝试了带参数和不带参数,但无济于事。错误发生在 ShowMessage 1 和 2 之间。

sName := ledName.Text;
sSurname := ledSurname.Text;
sSchool := ledSchool.Text;
sMotherName := ledMotherName.Text;
sMotherCell := ledMotherCell.Text;
sMotherEmail := ledMotherEmail.Text;
sFatherName := ledFatherName.Text;
sFatherCell := ledFatherCell.Text;
sFatherEmail := ledFatherEmail.Text;
sAddress := ledAddress.Text;
sBirthday := DateToStr(dpcBirthday.Date);
ShowMessage(sBirthday);
case rgpGender.ItemIndex of
  0 : cGender := 'M';
  1 : cGender := 'F';
end;
iGrade := rgpGrade.ItemIndex - 2;
if chkLeader.Checked = true then
  bLeader := True
else
  bLeader := False;
with dmData do begin
  ShowMessage('1');
  qryMain.Active := False;
  qryMain.SQL.Text := 'SELECT * FROM Users Where Name = "'+sName+'", Surname = "'+sSurname+'", Birthday = "'+sBirthday+'" ';
  qryMain.Open;
  if qryMain.RecordCount = 0 then begin
    qryMain.Close;
    ShowMessage('2');
    //qryMain.SQL.Text := 'INSERT INTO Users(Name, Surname, MotherName, FatherName, Gender, Grade, Birthday, School, Address, MotherCell, FatherCell, MotherEmail, FatherEmail, NameTag, Volunteer) VALUES("'+sName+'", "'+sSurname+'", "'+sMotherName+'", "'+sFatherName+'", "'+cGender+'", "'+IntToStr(iGrade)+'", "'+sBirthday+'", "'+sSchool+'", "'+sAddress+'", "'+sMotherCell+'", "'+sFatherCell+'", "'+sMotherEmail+'", "'+sFatherEmail+'", False, "'+BoolToStr(bLeader)+'") ';
    qryMain.SQL.Text := 'INSERT INTO Users(Name, Surname, MotherName, FatherName, Gender, Grade, Birthday, School, Address, MotherCell, FatherCell, MotherEmail, FatherEmail, NameTag, Volunteer) ' + 'VALUES(:Name, :Surname, :MotherName, :FatherName, :Gender, :Grade, :Birthday, :School, :Address, :MotherCell, :FatherCell, :MotherEmail, :FatherEmail, False, :Leader) ';
    qryMain.Parameters.ParamByName('Name').Value := sName;
    qryMain.Parameters.ParamByName('Surname').Value := sSurname;
    qryMain.Parameters.ParamByName('MotherName').Value := sMotherName;
    qryMain.Parameters.ParamByName('FatherName').Value := sFatherName;
    qryMain.Parameters.ParamByName('Gender').Value := cGender;
    qryMain.Parameters.ParamByName('Grade').Value := iGrade;
    qryMain.Parameters.ParamByName('Birthday').Value := sBirthday;
    qryMain.Parameters.ParamByName('School').Value := sSchool;
    qryMain.Parameters.ParamByName('Address').Value := sAddress;
    qryMain.Parameters.ParamByName('MotherCell').Value := sMotherCell;
    qryMain.Parameters.ParamByName('FatherCell').Value := sFatherCell;
    qryMain.Parameters.ParamByName('MotherEmail').Value := sMotherEmail;
    qryMain.Parameters.ParamByName('FatherEmail').Value := sFatherEmail;
    qryMain.Parameters.ParamByName('Leader').Value := bLeader;
    ShowMessage('3');
    qryMain.ExecSQL;
    qryMain.SQL.Text := 'SELECT * FROM Users';
    qryMain.Open;

注释掉的部分是我尝试这样做的一种方法,它给出了这个错误:

查询表达式 'Name="Derp",Surname="Foo",Birthday="1900-01-01"' 中的语法错误(逗号)

带参数的代码给了我这个错误:

查询表达式 'Name="Derp",Surname="Foo",Birthday="1900-01-01"' 中的语法错误(逗号)

任何帮助将不胜感激!

4

1 回答 1

2

Now that we can see your real code, your error is clear

'SELECT * FROM Users Where Name = "'+sName+
                    '", Surname = "'+sSurname+
                    '", Birthday = "'+sBirthday+'" ';

should use the AND operator to link those conditions :

'SELECT * FROM Users Where Name = "'+sName+
                 '" AND Surname = "'+sSurname+
                 '" AND Birthday = "'+sBirthday+'" ';

You should also, naturally, seriously consider parameterizing this query as well.

于 2014-08-21T11:04:02.913 回答