Define a standard database:
When defining a database, database file information and database structure information are required, such as the full path, database type, database field name, type, and length of the database file. In the Delphi Program, three standard types of databases can be defined: DBASE database, Paradox database, and ASCII database.
Use: tTable.
Procedure createdb ();
VaR Table1: tTable;
Begin
Table1: = tTable. Create (Self );
With Table1 do
Begin
Active: = false;
// Databasename: = "C:/dbtemp"; {pre-defined alias database workspace name}
Tablename: = 'ljh1. db'; {name of the database to be defined}
Tabletype: = ttparadox; {database type, which can be ttparadox, ttascii, ttdbase}
{If the tabletype attribute is ttdefault, it indicates that the database type corresponds to the extension of the specified value of tablename}
With fielddefs do {database structure information: This method adds a field to lw.db}
{Call the Add method of the tTable. fidlddefs object to add fields to the database table. Add has four parameters:
Field name: string.
Field Type: tfieldtype.
Field Size: Word. It is generally used only for string and memo types.
Whether the field is notnull: Boolean.
}
Begin
Clear;
Add ('yj', ftdate, 0, false );
Add ('zp', ftstring, 10, false); {Add a specific field name and type}
Add ('zdm', ftinteger, 0, false );
End;
With indexdefs do {This method adds an index field for lsung. DB}
{Use the tTable. indexdefs. Add () method to define the index. Add has three parameters:
Index name: string;
Index field name: string;
Index type: tindexoptions;
}
Begin
Clear;
Add ('yjindex', 'yj', [ixprimary]);
End;
Createtable;
End;
End;
Create with SQL statement:
Procedure sqlcreatedb ();
VaR Table2: tquery;
Begin
Table2: = tquery. Create (Self );
With Table2 do
Begin
With SQL do
Begin
Clear;
Add ('create table "ljh2.db "');
Add ('(YJ date,'); {Note the quotation marks (}
Add ('zp char (10 ),');
Add ('zdm INT) '); {Note the value in quotation marks )}
End;
Execsql;
SQL. Clear;
SQL. Add ('create index YJ on "lw.db" (YJ) '); {This SQL statement adds an index field for lw.db}
Execsql;
End;
End;
Database files ending with DB are generated in the current directory.
The above is the step for Delphi to create a memory table.
Kbmmemtable usage:
Kbmmemtable creation steps:
1. Create an kbmmemtable object instance.
2. Call the Add method of the kbmmemtable. fidlddefs object to add fields to the database table. The add method is the same as that of Delphi.
3. Use the kbmmemtable. indexdefs. Add () method to define the index. The add method is the same as that of Delphi.
4. Call createtable of kbmmemtable.
Important difference: kbmmemtable does not require BDE support. Therefore, do not specify the three attributes databasename, tablename, and tabletype.
The Code is as follows:
With kbmmemtable1 do
Begin
With kbmmemtable1.fielddefs do
Begin
Clear;
Add ('period ', ftinteger, 0, false );
Add ('value', ftlargeint, 0, false );
Add ('bytesfield ', ftbytes, 20, false );
Add ('color', ftinteger, 0, false );
Add ('date', ftdate, 0, false );
Add ('memo', ftmemo, 0, false );
Add ('autoinc', ftautoinc, 0, false );
End;
With kbmmemtable1.indexdefs do
Begin
Clear;
Add ('index1', 'value', []);
End;
Createtable;
End;
Comparison with Delphi in creating memory tables
Master/Slave table function:
Like other tdataset, kbmmemtable can be used to easily perform master-slave table operations by setting mastersource and masterfield.
SQL functions:
Kbmmemtable is not found to support SQL statement operations. It supports sorting by fields and searching for sorted fields.
Kbmmemtable features:
Obtain data from other tdataset.
The Code is as follows:
Loadfromdataset (Table1, [mtcpostructure, mtcpoproperties]);
In this way, kbmmemtable obtains all data from a DataSet object.
Function for saving and loading data in memory tables
The tTable of Delphi does not provide the savetofile function.
Kbmmemtable provides the ability to save to files. The saved files can be in either of the following formats:
Options: tkbmmemtable. saveflags;
1. binary format. Kbmmemtable. savetobinaryfile ('C:/test. bin', options ).
Kbmmemtable1.loadfrombinaryfile ('C:/test. bin ')
2. CSV format. Kbmmemtable. savetofile ('C:/test.csv ', options );
Kbmmemtable1.loadfromfile ('C:/test.csv ')
(A document format supported by Excel) The opened content is as follows:
@ File version @ 200
@ Tabledef start @@
Period = integer, 0, "period", "", 10
Value = largeint, 0, "value", "", 15
Bytesfield = bytes, 20, "bytesfield", "", 10
Color = integer, 0, "color", "", 10
Date = date, 0, "date", "", 10
Memo = memo, 0, "memo", "", 10
Autoinc = autoinc, 0, "autoinc", "", 10
Calc = string, 20, "calc", "", 20
@ Tabledef end @@
Period value bytesfield color date memo autoinc calc
1 198 0 02/11/2001 this is a memo % n2001-11-2 10:19:52 1 0 1-February
2 196 3 03/11/2001 this is a memo % n2001-11-2 10:19:52 2 1 2-March
The field structure of the table is described in the header of the document. below is the data area.