Reputation: 419
I'm trying to create a record in a database using a SQL query in Delphi 7. I'm using an ADO Query and I've tried both with and without parameters, but to no avail. The error occurs between ShowMessage 1 and 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;
The commented out part was the one way I tried doing this, and it gave this error:
Syntax error (comma) in query expression 'Name="Derp",Surname="Foo",Birthday="1900-01-01"'
The code with parameters gives me this error:
Syntax error (comma) in query expression 'Name="Derp",Surname="Foo",Birthday="1900-01-01"'
Any help would be greatly appreciated!
Upvotes: 1
Views: 933
Reputation: 31393
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.
Upvotes: 2