Sqlite (v) Data insertion, update and deletion under Delphi FIREDAC

Source: Internet
Author: User
Tags insert integer sqlite

Add first on a blank form: tfdconnection, Tfdphyssqlitedriverlink, Tfdguixwaitcursor, Tfdquery, Tdatasource, TDBGrid (and associated at design time). You can also copy the contents of the following text box and paste it directly onto the form to quickly complete the add process:Object Dbgrid1:tdbgrid left = top = Width = 361 Height = 329 DataSource = DataSource1 TabOrder = 0 Ti Tlefont.charset = Default_charset Titlefont.color = Clwindowtext Titlefont.height = -11 titlefont.name = ' Tahoma ' Titlefont.style = [] End object Fdconnection1:tfdconnection left = top =-end Object FDPHYSSQLITEDRIVERLINK1:TF Dphyssqlitedriverlink left = 143 top = "End Object Fdguixwaitcursor1:tfdguixwaitcursor" Provider = ' Forms ' Left = The top = "End" Object fdquery1:tfdquery Connection = FDConnection1 left = 344 top = The End object DataSource1 : Tdatasource DataSet = FDQuery1 left = 420 top = On end object Button1:tbutton left = top = Width = 7 5 Height = Caption = ' Button1 ' taborder = 1 OnClick = Button1Click End object Button2:tbutton left = T OP = 136 Width = Height = Caption = ' Button2 ' TabOrder = 2 OnClick = Button2click End Object Button3:tbut ton left = top = The Width = Height = Caption = ' Button3 ' taborder = 3 OnClick = Button3click End Object Button4:tbutton left = top = Equal Width = Height = Caption = ' Button4 ' taborder = 4 OnClick = Button4click End


More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Delphi/



Code:


{Create} 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)'; //: name,: age The Format function is also handy.
  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;

{Remove} 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.