Unknown column in field list

本秂侑毒 提交于 2020-07-03 07:51:56

问题


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?


回答1:


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]);

In the original version of my answer, I explained how to fix your problem by "doubling up" single quotes in the Sql you were trying to build, because it seemed to me that you were having difficulty seeing (literally) what was wrong with what you were doing.

An alternative (and better) way to avoid your problem (and the one I always use in real life) is to use the QuotedStr() function. The same code would then become

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

According to the Online Help:

Use QuotedStr to convert the string S to a quoted string. A single quote character (') >is inserted at the beginning and end of S, and each single quote character in the string is >repeated.

What it means by "repeated" is what I've referred to as "doubling up". Why that's important, and the main reason I use QuotedStr is to avoid the Sql db-engine throwing an error when the value you want to send contains a single quote character as in O'Reilly.

Try adding a row containing that name to your table using MySql Workbench and you'll see what I mean.

So, not only does using QuotedStr make constructing SQL statements as strings in Delphi code less error-prone, but it also avoid problems at the back-end, too.




回答2:


Just in case this will help anybody else I had the same error when I was parsing a python variable with a sql statement and it had an if statement in i.e.

sql="select bob,steve, if(steve>50,'y','n') from table;"

try as I might it coming up with this "unknown column y" - so I tried everything and then I was about to get rid of it and give it up as a bad job until I thought I would swap the " for ' and ' for "..... Hoooraaahh it works! This is the statement that worked

sql='select bob,steve, if(steve>50,"y","n") from table;'

Hope it helps...




回答3:


To avoid this sort of problem and SQL injection you should really look into using SQL parameters for this, not the Pascal format statement.



来源:https://stackoverflow.com/questions/38253421/unknown-column-in-field-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!