Lazarus

Programming => Databases => Topic started by: guest48180 on May 31, 2018, 06:15:46 am

Title: [SOLVED] Is it possible to use parameters with ExecuteDirect?
Post by: guest48180 on May 31, 2018, 06:15:46 am
I have a lot of text fields on a form. One of them is txt_Recipe_Name. I can't find documentation through the Forum Search or anywhere else on how to use parameters with ExecuteDirect. I can use them with SQLQuery but not with ExecuteDirect. Is it possible to use parameters with ExecuteDirect? Or is there another way to accomplish an Insert on a table and be able to write my form data to the table fields?

This is what I have that I can't figure out how to make work:
Code: Pascal  [Select]
  1. SQLite3Connection1.ExecuteDirect('INSERT INTO recipe_names(name) VALUES(:r_name);');
How can I set :r_name to a text field (TEdit field) on my form?

For a SELECT statement, this works nicely:
Code: Pascal  [Select]
  1. SQLQuery2.SQL.Text:='SELECT * FROM recipe_ingredients WHERE name_id = :id';
  2. SQLQuery2.ParamByName('id').AsInteger:=SQLQuery1.FieldByName('name_id').AsInteger;

I use Prepared statements in PHP for all my MariaDB queries. But I can't find information on how to use them on databases within Lazarus. A wiki maybe?  :)

In any case, my main concern is being able to use data on my form to INSERT INTO an SQLite3 db. Anyone?
Title: Re: Is it possible to use parameters with ExecuteDirect?
Post by: guest48180 on May 31, 2018, 07:56:13 am
Thank you, Juassic Park. All I've seen used on CREATE, INSERT, and UPDATE was ExecuteDirect. Your answer (https://forum.lazarus.freepascal.org/index.php/topic,41108.msg284688.html#msg284688) showed me different. Thanks.
Title: Re: [SOLVED] Is it possible to use parameters with ExecuteDirect?
Post by: mangakissa on May 31, 2018, 09:13:03 am
which answer?
Title: Re: [SOLVED] Is it possible to use parameters with ExecuteDirect?
Post by: guest48180 on May 31, 2018, 09:49:11 am
In the link @answer (https://forum.lazarus.freepascal.org/index.php/topic,41108.msg284688.html#msg284688) here and in my above post.
Title: Re: [SOLVED] Is it possible to use parameters with ExecuteDirect?
Post by: kapibara on May 31, 2018, 11:59:19 am
SqlDB's ExecuteDirect doesn't work with parameters.

https://www.freepascal.org/docs-html/fcl/sqldb/tsqlconnection.executedirect.html
Title: Re: [SOLVED] Is it possible to use parameters with ExecuteDirect?
Post by: guest48180 on May 31, 2018, 12:15:12 pm
Quote
ExecuteDirect does not offer support for parameters, so only statements that do not need parsing and parameters substitution can be handled. If parameter substitution is required, use a TSQLQuery component and its ExecSQL method.

Thanks, kapibara.