mohsen.2 mohsen.2 - 4 months ago 17
MySQL Question

Unknown column in field list

I'm trying to insert some information to MySQL with Pascal, but when I run the program I get the error


unknown column 'mohsen' in field list


This is my code

procedure TForm1.Button1Click(Sender: TObject);
var
aSQLText: string;
aSQLCommand: string;
namee:string;
family:string;
begin
namee:='mohsen';
family:='dolatshah';
aSQLText:= 'INSERT INTO b_tbl(Name,Family) VALUES (%s,%s)';
aSQLCommand := Format(aSQLText, [namee, family]);
SQLConnector1.ExecuteDirect(aSQLCommand);
SQLTransaction1.Commit;
end;


How can I solve this problem?

Answer

It's because your

VALUES (%s,%s)

isn't surrounding the namee and family variable contents by quotes. Therefore, your back-end Sql engine thinks your mohsen is a column name, not a value.

Instead, use, e.g.

VALUES (''%s'',''%s'')

as in

  Namee := 'mohsen';
  Family := 'dolatshah';
  aSQLText:= 'INSERT INTO b_tbl(Name,Family) VALUES (''%s'',''%s'')';
  aSQLCommand := Format(aSQLText,[namee,family]);