建立數據庫的代碼:
{建立內存數據庫的一般代碼:}
begin
FDConnection1.DriverName := 'SQLite'; //同 FDConnection1.Params.Add('DriverID=SQLite');
// FDConnection1.Params.Add('Database=:memory:'); //可省略這行, FireDAC 的源碼顯示, if Database = '' then Database := ':memory:';
// FDConnection1.Params.Add('SQLiteAdvanced=page_size=4096'); //可指定內存頁大小, 這是默認值
FDConnection1.Connected := True;
end
{建立文件數據庫的一般代碼:}
begin
FDConnection1.Params.Add('DriverID=SQLite');
FDConnection1.Params.Add('Database=C:\Temp\New1.sdb'); //如果文件存在就打開, 不存在就建立
// FDConnection1.Params.Add('SQLiteAdvanced=temp_store=Memory'); //可強制臨時文件在內存以提高效率. 0:DEFAULT; 1:FILE; 2:MEMORY
// FDConnection1.Params.Add('SQLiteAdvanced=temp_store_directory=C:\Temp'); //默認的臨時文件路徑應該是 C:\Documents and Settings\user-name\Local Settings\Temp\
// FDConnection1.Params.Add('OpenMode=CreateUTF8'); //默認是 CreateUTF8, 也可選擇 CreateUTF16
// FDConnection1.Params.Add('LockingMode=Normal'); //默認是多用戶模式, 如果使用獨占模式 LockingMod=Exclusive 會更有效率
FDConnection1.Connected := True;
end;
所有建立參數參見: http://www.sqlite.org/pragma.html
先在空白窗體上添加: TFDConnection、TFDPhysSQLiteDriverLink、TFDGUIxWaitCursor; 數據庫的建立主要通過 TFDConnection 完成.
同時添加用於呈現數據的 TFDQuery、TDataSource、TDBGrid, 還要添加一個 TFDCommand 用於提交建表命令, 然后調整如下屬性:
FDQuery1 . Connection = FDConnection1 DataSource1 . DataSet = FDQuery1 DBGrid1 . DataSource = DataSource1 FDCommand1 . Connection = FDConnection1
你可以復制下面文本框中的內容, 然后直接往窗體上貼, 以快速完成以上的添加過程:
測試代碼:
procedure TForm1.FormCreate(Sender: TObject);
const
dbPath = 'C:\Temp\SQLiteTest.sdb';
begin
if FileExists(dbPath) then DeleteFile(dbPath);
with FDConnection1 do begin
Params.Add('DriverID=SQLite');
Params.Add('Database=' + dbPath);
Connected := True;
end;
{創建一個名為 MyTable 的表, 字段包括: ID, Name, Age, Note, Picture}
with FDCommand1.CommandText do begin
Add('CREATE TABLE MyTable(');
Add('ID integer PRIMARY KEY,'); //Integer 類型, 同時設為主鍵
Add('Name string(10),'); //能容下 10 個字符的 String 類型
Add('Age byte,'); //Byte 類型
Add('Note text,'); //Memo 類型
Add('Picture blob'); //Blob(二進制)類型
Add(')');
end;
FDCommand1.Active := True;
{查看表}
FDQuery1.Open('SELECT * FROM MyTable');
end;
效果圖:
直接使用 TFDConnection 提交 DDL 命令更簡單:
procedure TForm1.FormCreate(Sender: TObject);
const
dbPath = 'C:\Temp\SQLiteTest.sdb';
begin
if FileExists(dbPath) then DeleteFile(dbPath);
with FDConnection1 do begin
Params.Add('DriverID=SQLite');
Params.Add('Database=' + dbPath);
Connected := True;
end;
{創建一個名為 MyTable 的表, 字段包括: ID, Name, Age, Note, Picture}
FDConnection1.ExecSQL('CREATE TABLE MyTable(ID integer PRIMARY KEY, Name string(10), Age byte, Note text, Picture blob)');
{查看表}
FDQuery1.Open('SELECT * FROM MyTable');
end;
使用 SQLite 底層包裝完成的建表提交(這樣應該更有效率):
uses FireDAC.Phys.SQLiteWrapper; //為使用 TSQLiteStatement
{使用 TSQLiteStatement 完成的提交 SQL 命令的函數}
procedure MyExecSQL(ACon: TFDConnection; const ASQL: String);
begin
with TSQLiteStatement.Create(ACon.CliObj) do
try
Prepare(ASQL);
Execute;
while PrepareNextCommand do Execute;
finally
Free;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
const
dbPath = 'C:\Temp\SQLiteTest.sdb';
begin
if FileExists(dbPath) then DeleteFile(dbPath);
with FDConnection1 do begin
Params.Add('DriverID=SQLite');
Params.Add('Database=' + dbPath);
Connected := True;
end;
{創建一個名為 MyTable 的表, 字段包括: ID, Name, Age, Note, Picture}
MyExecSQL(FDConnection1, 'CREATE TABLE MyTable(ID integer PRIMARY KEY, Name string(10), Age byte, Note text, Picture blob)');
{查看表}
FDQuery1.Open('SELECT * FROM MyTable');
end;
關於數據類型, SQLite 本身只支持(Null, Integer, Real, Text, Blob), 但我們可以放心使用 Delphi 的大多數類型(也包括 Delphi 沒有的), 因為 FireDAC 幕后做了轉換工作.
SQLite 到 FireDAC 數據類型映射表: ( http://docwiki.embarcadero.com/RADStudio/XE6/en/Using_SQLite_with_FireDAC)
| Type name | Description |
|---|---|
| rowid | _rowid_ | oid | dtInt64, Attrs = [caSearchable, caAllowNull, caROWID] |
| bit | bool | boolean | logical | yesno | dtBoolean |
| tinyint | shortint | int8 [unsigned] | dtSByte / dtByte |
| byte | uint8 | dtByte |
| smallint | int16 [unsigned] | dtInt16 / dtUInt16 |
| word | uint16 | year | dtUInt16 |
| mediumint | integer | int | int32 [unsigned] | dtInt32 / dtUInt32 |
| longword | uint32 | dtUInt32 |
| bigint | int64 | counter | autoincrement | identity [unsigned] | dtInt64 / dtUInt64 |
| longlongword | uint64 | dtUInt64 |
| real | float | double | dtDouble |
| single [precision] [(p, s)] | dtSingle / dtBCD / dtFmtBCD |
| decimal | dec | numeric | number [unsigned] [(p, s)] | dtSByte / dtInt16 / dtInt32 / dtInt64 dtByte / dtUInt16 / dtUInt32 / dtUInt64 dtBCD / dtFmtBCD |
| money | smallmoney | currency | financial [(p, s)] | dtCurrency |
| date | smalldate | dtDate |
| datetime | smalldatetime | dtDateTime |
| timestamp | dtDateTimeStamp |
| time | dtTime |
| char | character [(l)] | dtAnsiString, Len = L, Attrs = [caFixedLen] |
| varchar | varchar2 | tynitext | character varying | char varying [(l)] | dtAnsiString, Len = L |
| nchar | national char | national character [(l)] | dtWideString, Len = L, Attrs = [caFixedLen] |
| nvarchar | nvarchar2 | national char varying | string [(l)] | dtWideString, Len = L |
| raw | tyniblob | varbinary | binary | binary varying [(l)] | dtByteString, Len = L |
| blob | mediumblob | image | longblob | long binary | long raw | longvarbinary | general | oleobject | tinyblob | dtBlob |
| mediumtext | longtext | clob | memo | note | long | long text | longchar | longvarchar | tinytext | dtMemo |
| text | ntext | wtext | nclob | nmemo | long ntext | long wtext | national text | longwchar | longwvarchar | html | dtWideMemo |
| xmldata | xmltype | xml | dtXML |
| guid | uniqueidentifier | dtGUID |
| other data types | dtWideString |
