To build the code for the database:
{General code to establish a Memory database:}beginFdconnection1.drivername: = ' SQLite '; With FDConnection1.Params.Add (' driverid=sqlite ');//FDConnection1.Params.Add (' database=:memory: '); Can omit this line, FIREDAC source code shows that if database = ' then database: = ': Memory: ';//FDConnection1.Params.Add (' Sqliteadvanced=page_siz e=4096 '); You can specify the memory page size, which is the default value fdconnection1.connected: = True; End{General code for establishing a file database:}beginFDCONNECTION1.PARAMS.ADD (' Driverid=sqlite '); FDCONNECTION1.PARAMS.ADD (' Database=c:\temp\new1.sdb '); If the file exists, it does not exist, it is created
FDCONNECTION1.PARAMS.ADD (' sqliteadvanced=temp_store=memory '); You can force temporary files to be in memory for increased efficiency. 0:default; 1:file; 2:memory
FDCONNECTION1.PARAMS.ADD (' sqliteadvanced=temp_store_directory=c:\temp '); The default temporary file path should be C:\Documents and settings\user-name\local settings\temp\
FDCONNECTION1.PARAMS.ADD (' Openmode=createutf8 '); The default is CreateUTF8, you can also choose createutf16//FDConnection1.Params.Add (' lockingmode=normal '); The default is multiuser mode, which is more efficient if you use exclusive mode lockingmod=exclusive fdconnection1.connected: = True; End;
All set up parameters See also: http://www.sqlite.org/pragma.html
First add on the blank form: tfdconnection, Tfdphyssqlitedriverlink, tfdguixwaitcursor; The establishment of database is mainly accomplished by tfdconnection. Add the Tfdquery, Tdatasource, TDBGrid that are used to render the data, add a tfdcommand to submit the build command, and then adjust the following properties:
FDQuery1 . Connection = FDConnection1
DataSource1. DataSet = FDQuery1
DBGrid1 . DataSource = DataSource1
FDCommand1 . Connection = FDConnection1
You can copy the contents of the following text box and paste it directly onto the form to quickly complete the addition process: object Fdconnection1:tfdconnection left = Upper = End Object Fdphyss Qlitedriverlink1:tfdphyssqlitedriverlink left = 143 top = The End object Fdguixwaitcursor1:tfdguixwaitcursor Provid er = ' Forms ' left = the top = "End object Fdquery1:tfdquery Connection = FDConnection1 left = Object Datasource1:tdatasource DataSet = FDQuery1 left = 148 top = m-End object Fdcommand1:tfdcommand connecti On = FDConnection1 left = 264 top = "End Object Dbgrid1:tdbgrid" = top = 144 Width = 409 Height = 13 7 DataSource = DataSource1 TabOrder = 0 Titlefont.charset = Default_charset titlefont.color = Clwindowtext Title Font.height = -11 titlefont.name = ' Tahoma ' Titlefont.style = [] end
Test code:
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 ;
{Create a table named MyTable, with fields including: ID, name, age, note, picture} with
Fdcommand1.commandtext do begin Add (' CREATE TABLE MyTable ('); ADD (' ID integer PRIMARY KEY, '); Integer type, set as primary key Add (' Name string (10), '); Can accommodate 10 characters of String type Add (' Age byte, '); Byte type Add (' Note text, '); Memo type Add (' Picture blob ');
Blob (binary) type Add (') ');
End ;
Fdcommand1.active: = True;
{View Table}
Fdquery1.open (' SELECT * from MyTable ');
End ;
Effect Chart:
It is simpler to submit DDL commands directly using tfdconnection:
procedure Tform1.formcreate (sender:tobject);
Const
DBPath = ' C:\Temp\SQLiteTest.sdb ';
begin
if then DeleteFile (dbpath);
with Todo begin Params.add (' Driverid=sqlite ');
Params.add (' database= ' + dbpath);
Connected: = True;
end;
{Create a table named MyTable, with fields including: ID, name, age, note, picture}
Fdconnection1.execsql (' CREATE TABLE MyTable (ID integer PRIMARY KEY, Name string (), age byte, note text, picture blob) ');
{view Table}
Fdquery1.open (' SELECT * from MyTable ');
end;