Recent

Author Topic: Add variable from application to MySQL Database  (Read 1990 times)

criss6100

  • New Member
  • *
  • Posts: 24
Add variable from application to MySQL Database
« on: January 28, 2015, 05:58:20 pm »
Hello, I want to add a variable to a MySQL Database, I can read data from the Database, but I can't seem to find a way to add, for example I have a field named "Age" in my application and I write for example "14", how can I add said "14" into the database, it being a variable?
I tried to convert the variable into a string after it has been written, but it doesn't work or I don't do it correctly, did a google search but nothing helpful came up...
I can provied more info if needed

Thank you :)
« Last Edit: January 28, 2015, 06:01:20 pm by criss6100 »

balazsszekely

  • Guest
Re: Add variable from application to MySQL Database
« Reply #1 on: January 29, 2015, 05:49:51 am »
The keyword is parameterized query.
Lets say you have a TMySQL55Connection, TSQLQuery and TSQLTransaction on your form.
Code: [Select]
  //...
  SQLQuery1.SQL.Text := 'update PEOPLE set AGE = :pAGE where ID = :pID';
  SQLQuery1.ParamByName('pAGE').AsInteger := 87;
  SQLQuery1.ParamByName('pID').AsInteger := 10;
  SQLQuery1.ExecSQL;
  SQLTransaction1.CommitRetaining;
  //...

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Add variable from application to MySQL Database
« Reply #2 on: January 29, 2015, 08:55:00 am »
That's an opportunity, but if the dataset is already loaded use this:
Code: [Select]
//......
SQLquery1.post;
SQLquery1.applyupdates;
SQLTransaction.CommitRetaining  //Commit closes the dataset.
//....
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

 

TinyPortal © 2005-2018