Page 1 of 1

Am I dumb ? - WideString to SQL Insert

Posted: Fri 22 Oct 2010 12:01
Hi - my brain is like birds - since hours I try to put some Data to Server but always error.
The Field in DB is a text field
I will Insert a widestring like with the code below...
but the windestring is always cut - and then error.
(when I execute the insert quers direct on server - all is ok)

procedure TForm1.Button1Click(Sender: TObject);
var xxx : WideString;
begin
xxx := 'ökjdfgh ökasj dfkjöhg ösdkjfhg öskdjfhg öksjdfhg öskdjfg dsöjkfgh ';
xxx := xxx + xxx + xxx + xxx+ xxx+ xxx+ xxx+ xxx+ xxx+ xxx+ xxx+ xxx+ xxx;
Myquery1.Sql.Text := 'INSERT INTO products_description '+
' (products_id, language_id, products_name, products_description) VALUES ' +
'(88889, 2, "ewewewewewewewe",'+xxx+'")';

myquery1.Execute;

end;

Posted: Fri 22 Oct 2010 14:01
by AndreyZ
Hello,

You forgot to quote the SQL code for the xxx string. The following SQL code is correct:

Code: Select all

  MyQuery.SQL.Text := 'INSERT INTO products_description (products_id, language_id, products_name, products_description) VALUES (88889, 2, "ewewewewewewewe","' + xxx + '")';
Also you can use parameters (it's a better way) in the following way:

Code: Select all

  MyQuery.SQL.Text := 'INSERT INTO products_description (products_id, language_id, products_name, products_description) VALUES (:products_id, :language_id, :products_name, :products_description)';
  MyQuery.ParamByName('products_description').AsString := xxx;

Posted: Fri 22 Oct 2010 14:11
Thanks...
ahhhhh now 100 lamps are going to burn....!!

Merci

MyQuery.SQL.Text := 'INSERT INTO products_description (products_id, language_id, products_name, products_description) VALUES (:products_id, :language_id, :products_name, :products_description)';
MyQuery.ParamByName('products_description').AsString := xxx;
MyQuery.ParamByName('products_id').AsString := myidvariable;
MyQuery.ParamByName('products_id').AsString := mylaguagevariable;


Sorry one more:
and when I will have a WHERE ?
eg WHERE products_id = myProductID

Posted: Mon 25 Oct 2010 07:25
by AndreyZ
You can use WHERE clause in the following way:

Code: Select all

  MyQuery.Sql.Clear;
  MyQuery.Sql.Add('select * from products_description');
  MyQuery.Sql.Add('where products_id = :myProductID');
  MyQuery.ParamByName('myProductID').AsInteger := 1;
  MyQuery.Open;