Sqlite under FireDAC [5]-Inserting, updating, deleting data

Source: Internet
Author: User



First add on the blank form: tfdconnection, Tfdphyssqlitedriverlink, Tfdguixwaitcursor, Tfdquery, Tdatasource, TDBGrid (and associated at design time).

You can also copy the contents of the text box below and paste it directly onto the form to quickly complete the add-in process:
Object Dbgrid1:tdbgrid left = all Top = Width = 361 Height = 329 DataSource = DataSource1 TabOrder = 0 Titlefont . Charset = Default_charset Titlefont.color = Clwindowtext Titlefont.height = -11 titlefont.name = ' Tahoma ' titlefont.st YLE = []endobject fdconnection1:tfdconnection left = Top = 24endobject Fdphyssqlitedriverlink1:tfdphyssqlitedriverl Ink left = 143 top = 24endobject fdguixwaitcursor1:tfdguixwaitcursor Provider = ' Forms ' left = 260 top = 24endobject Fdquery1:tfdquery Connection = FDConnection1 left = 344 Top = 24endobject Datasource1:tdatasource DataSet = fdquery 1 left = 420 top = 24endobject Button1:tbutton left = top = Width = Height = Caption = ' Button1 ' Tab Order = 1 OnClick = Button1clickendobject Button2:tbutton left = Top = 136 Width = Height = Caption = ' Bu Tton2 ' TabOrder = 2 OnClick = Button2clickendobject Button3:tbutton left = Top = 192 Width = Height = Ca ption = ' Button3 ' TabORder = 3 OnClick = Button3clickendobject Button4:tbutton left = Top = Width = Height = + Caption = ' but Ton4 ' TabOrder = 4 OnClick = Button4clickendend
Code:
{set up}
procedure TForm1.FormCreate (Sender: TObject);
const
  dbPath = ‘C: \ Temp \ SQLiteTest.sdb’;
  strTable = ‘CREATE TABLE MyTable (Id integer PRIMARY KEY AUTOINCREMENT, Name string (10), Age byte)’; // Id, Name, Age three fields
                                                                                                      // integer PRIMARY KEY AUTOINCREMENT: Increment field
begin
  if FileExists (dbPath) then DeleteFile (dbPath);

  FDConnection1.ConnectionString: = ‘DriverID = SQLite; Database =‘ + dbPath;
  FDConnection1.ExecSQL (strTable);

  FDQuery1.Open (‘SELECT * FROM MyTable’);
end;

{insert}
procedure TForm1.Button1Click (Sender: TObject);
const
  strInsert = ‘INSERT INTO MyTable (Name, Age) VALUES (: name,: age)’;
begin
  FDConnection1.ExecSQL (strInsert, [‘AAA‘, 11]);
  FDConnection1.ExecSQL (strInsert, [‘BBB‘, 22]);
  FDConnection1.ExecSQL (strInsert, [‘CCC’, 33]);
  FDConnection1.ExecSQL (strInsert, [‘DDD’, 44]);
  FDConnection1.ExecSQL (strInsert, [‘EEE’, 55]);
  FDQuery1.Refresh;
end;

{Update}
procedure TForm1.Button2Click (Sender: TObject);
begin
  FDConnection1.ExecSQL (‘UPDATE MyTable SET Age =: a WHERE Name =: n’, [Random (100), ‘AAA’]);
  FDQuery1.Refresh;
end;

{delete}
procedure TForm1.Button3Click (Sender: TObject);
begin
  FDConnection1.ExecSQL (‘DELETE FROM MyTable WHERE Age> 33’);
  FDQuery1.Refresh;
end;

{Query the first result that meets the conditions}
procedure TForm1.Button4Click (Sender: TObject);
var
  V: Variant;
begin
  V: = FDConnection1.ExecSQLScalar (‘SELECT Age FROM MyTable WHERE Name =: x’, [‘BBB‘]);
  ShowMessage (V);
end; 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.