Am I dumb ? - WideString to SQL Insert

Discussion of open issues, suggestions and bugs regarding MyDAC (Data Access Components for MySQL) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
[email protected]
Posts: 5
Joined: Tue 05 Oct 2010 09:13
Location: CH-Zürich

Am I dumb ? - WideString to SQL Insert

Post by [email protected] » 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;

AndreyZ

Post by AndreyZ » Fri 22 Oct 2010 14:01

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;

[email protected]
Posts: 5
Joined: Tue 05 Oct 2010 09:13
Location: CH-Zürich

Post by [email protected] » 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

AndreyZ

Post by AndreyZ » Mon 25 Oct 2010 07:25

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;

Post Reply