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?
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.
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...
To avoid this sort of problem and SQL injection you should really look into using SQL parameters for this, not the Pascal format statement.
When using SQLStatement objects in Adobe Flex I can use parameters which I can substitute with values later. I have a SQL statement like the following:
SELECT x
FROM y
WHERE z IN (:ids);
I would like to substitute :ids with a list which can have any number of elements. I try assigning an Array to SQLStatement.parameters[":ids"] but the condition fails, causing me to believe that the Array is not translated properly into an SQL list. The Array I try to assign contains Strings.
Is there a way to solve this?
Can't you create a comma seperated String?
I have a table named CASE.
And let's say I have the ff code:
declare
a CASE%rowtype;
begin
null;
end;
The above code will throw the error: PLS-00103: Encountered the symbol "CASE" when expecting one of the following: ...
Is there anyway for me to create a variable of CASE%rowtype without manually creating the datatype itself? Thanks!
as Case is a reserved keyword,
you can escape it with double quotes
a "CASE"%rowtype;
useless to say that you should avoid reserved keywords in object naming... (or not that useless ?)
Why when i pass this parameter to sqldatasource parameters i see the correct defaultvalue but an incorrect parameter value? i don't know the difference between these two types of parameters, i Always think that default value is what is for my interest. I want to send in DB value 7.5 and not 75, could anyone help me please?
Parameter prm_prezzo = new Parameter("cc_prezzo", DbType.Decimal, newValues["PREZZO"].ToString().Replace(",", "."));
Solved but i would an expert's think about this because it's not clear for me:
the first instruction that i post had the replace of "," with "." because without the replace the "," escape the stored procedure
doing so...
Parameter prm_prezzo = new Parameter("cc_prezzo", DbType.Decimal);
prm_prezzo.DefaultValue = newValues["PREZZO"].ToString();
...i have the correct value and the stored procedure is not escaped...
but i don't undestand the difference between the two methods :-0
I'm currently working on a project that was written in classic asp. I've used this language some before but I'm rusty with it.
In that code I see the following function call:
Result = SwapOEMPart(sItem)
When I look at SwapOEMPart I see this:
function SwapOEMPart(oemPart)
// Do a bunch of stuff
oemPart = objRS("CCIPartNo") <-- this is the result of the stuff
end function
What does that do? Does it fill Result with the value of oemPart? Does it change the value of sItem (similar to a pass by reference)? Or perhaps it is something entirely different.
I'm familiar with returning data from asp functions by setting the function name equal to the value you want to return, but in this instance they are changing the value of the parameter they pass in and then just ending the function.
Based on the code you have provided, I'm going to assume objRS is an adodb.recordset, if that is the case, CCIPartNo is a column in the recorset, all your code is doing is writing the value of that column into the eomPart variable - eomPart isnt referenced as byref in the function declaration but this is assumed as default if you're in vbscript (not .net) so **it's almost as if the value of the column is being passed back into eomPart & because eomPart is a REFERENCE to the sItem value in your example, the actual value of sItem would change.
http://msdn.microsoft.com/en-us/library/ee478101%28VS.84%29.aspx