zoukankan      html  css  js  c++  java
  • FireDAC 下的 Sqlite [5]


    先在空白窗体上添加: TFDConnection、TFDPhysSQLiteDriverLink、TFDGUIxWaitCursor、TFDQuery、TDataSource、TDBGrid(并在设计时关联好).

    你也可以复制下面文本框中的内容, 然后直接往窗体上贴, 以快速完成以上的添加过程:


    代码:
    {建立}
    procedure TForm1.FormCreate(Sender: TObject);
    const
      dbPath = 'C:TempSQLiteTest.sdb';
      strTable = 'CREATE TABLE MyTable(Id integer PRIMARY KEY AUTOINCREMENT, Name string(10), Age byte)'; //Id, Name, Age 三个字段
                                                                                                          //integer PRIMARY KEY AUTOINCREMENT: 自增字段
    begin
      if FileExists(dbPath) then DeleteFile(dbPath);
    
      FDConnection1.ConnectionString := 'DriverID=SQLite; Database=' + dbPath;
      FDConnection1.ExecSQL(strTable);
    
      FDQuery1.Open('SELECT * FROM MyTable');
    end;
    
    {插入}
    procedure TForm1.Button1Click(Sender: TObject);
    const
      strInsert = 'INSERT INTO MyTable(Name, Age) VALUES(:name, :age)'; //:name, :age 的方式(后面还要以数组的方式给出相应的值), 这比字符串的 Format 函数还要方便.
    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;
    
    {更新}
    procedure TForm1.Button2Click(Sender: TObject);
    begin
      FDConnection1.ExecSQL('UPDATE MyTable SET Age=:a WHERE Name=:n', [Random(100), 'AAA']);
      FDQuery1.Refresh;
    end;
    
    {删除}
    procedure TForm1.Button3Click(Sender: TObject);
    begin
      FDConnection1.ExecSQL('DELETE FROM MyTable WHERE Age>33');
      FDQuery1.Refresh;
    end;
    
    {查询符合条件的第一个结果}
    procedure TForm1.Button4Click(Sender: TObject);
    var
      V: Variant;
    begin
      V := FDConnection1.ExecSQLScalar('SELECT Age FROM MyTable WHERE Name = :x', ['BBB']);
      ShowMessage(V);
    end;
    
    //:
    select * from test where name = 'ABCDE' COLLATE NOCASE;
    create table test (id Integer,name Text COLLATE NOCASE );
    


  • 相关阅读:
    CentOS6.8上Docker的安装
    IDE- VS Code-插件-Golang
    Tool-Docker-First exploration
    C++-Code-Time Transfer-Windows FILETIME(1601) To 1970 UTC
    Tool-git-command-入门笔记[慕课网-五月的夏天]
    C++-当表达式中同时存在有符号和无符号的类型时,有符号类型先转为无符号参与计算
    C语言-C语言程序设计-Practice code
    C语言-C语言程序设计-Function-strcpy
    C语言-C语言程序设计-Function-fopen
    C语言-C语言程序设计-Application-逆波兰计算器
  • 原文地址:https://www.cnblogs.com/del/p/3742423.html
Copyright © 2011-2022 走看看