FireDAC中的SQLite(二)

咱們接下來將要使用FDDemo.sdb數據庫進行訪問,開始咱們的第一個SQLite訪問例子。數據庫

咱們的FDDemo.sdb存放目錄在:C:\Program Files (x86)\Embarcadero\Studio\14.0\Samples\Dataspa

新建VCL Forms Application

在XE6 IDE中按 Ctrl + . 鍵,快速輸入 VCL Forms Application,回車後新建了一個VCL Forms Application應用程序。code

添加FireDAC相關組件

繼續按Ctrl + . 鍵,快速輸入下面的字符,建立以下組件:orm

FDPhysSQLiteDriverLink    //SQLite驅動自動鏈接組件,等同於 uses FireDAC.Phys.SQLite;
FDGUIxWaitCursor          //用於自動管理GUI程序的等待光標,等同於 uses FireDAC.VCLUI.Wait;
FDConnection              //數據鏈接
FDQuery                   //數據查詢
DataSource                //數據源
DBGrid                    //數據表格顯示

添加後,IDE中以下圖效果:blog

SQLite003

 

接下來咱們輸入簡單的代碼,以下:get

procedure TForm1.FormCreate(Sender: TObject);
var
  DBStr: string;
begin
  //設置FDDemo.sdb的路徑
  DBStr := 'C:\Program Files (x86)\Embarcadero\Studio\14.0\Samples\Data\FDDemo.sdb';
  FDConnection1.DriverName := 'SQLite';
  FDConnection1.Params.Add('Database=' + DBStr);

  FDQuery1.Connection := FDConnection1;
  DataSource1.DataSet := FDQuery1;
  DBGrid1.DataSource := DataSource1;

  FDQuery1.SQL.Text := 'SELECT * FROM Products';
  FDQuery1.Open();
  FDConnection1.Open();

  DBGrid1.Align := alClient;
end;

 

OK,至此,咱們已經完成了一個SQLite的小例子了,按F9開始運行吧,運行效果以下:string

SQLite004

 

經過看萬一老師的代碼,發現FDConnection可使用ConnectionString設置鏈接內容。it

  //FDConnection1.DriverName := 'SQLite';
  //FDConnection1.Params.Add('Database=' + DBStr); 等同於下面一行代碼

  FDConnection1.ConnectionString := 'DriverID=SQLite; Database=' + DBStr;

固然FDConnection.Open方法和FDQuery.Open方法支持以下使用:io

  FDConnection1.Open('DriverID=SQLite; Database=' + DBStr);
  FDQuery1.Open('SELECT * FROM Products');

Open方法中直接賦予ConnectionString和SQL語句。class

相關文章
相關標籤/搜索