1. The status of database alias
Delphi's built-in database engine BDE (Borland Database Engine), provides powerful database processing functions, and BDE is the core part of Delphi's database function.
Taking a local database that is only used by a stand-alone machine as an example, the Delphi program can directly control the access of database data through BDE. This access control is based on the configuration of BDE parameters. Specifically, the database alias is correctly set. Delphi's most commonly used database access component TTable, and its attribute DataBaseName is the defined database alias. , without defining a database alias, you cannot access specific database tables. Therefore, when creating a database table, there must be two processes: one is to detect whether the corresponding database alias exists through BDE. If it does not exist, the corresponding database alias should be established first; the other is to detect the database alias (i.e. DataBaseName) in the database alias (i.e. DataBaseName) Whether there is a database table that needs to be established, if it does not exist, it will be established. In this way, the data in the original table is protected from loss.
2. Session component: Interface between Delphi database program and BDE
Delphi automatically provides a Session component for database applications, which is the key to the application's interface with BDE. Below is a prototype description of several processes of the Session component regarding the BDE interface.
1.GetAliasNames(list:Tstrings); Store the names of all database alias in the current BDE configuration in the List string list.
2.AddStandardAlias(const Name, Path, DefaultDriver: string); Add a standard type of database alias. For example, add an alias named Cntssamp, the default database driver is PARADOX, and the storage path is c:/delphp11:
Session.AddStandardAlias(Cntssamp,c:/delphp11,Paradox);
3.GetTableNames(const DatabaseName, Pattern: string; Extensions,SystemTables: Boolean; List: TStrings); You can store the names of all tables in the specified database alias in the List string list.
4.SaveConfigFile; Save BDE configuration to drive.
In addition, use the value of List.IndexOf (specified string) to determine whether there are required strings in the string list to determine whether a new database alias and database table should be established.
3. An example
Write the following code in the Form1.FormCreate event. When the program starts, it will automatically detect whether the database alias Cntssamp exists. If not, it will be created. It will automatically detect whether the table TSK (Library) exists in the alias Cntssamp. If not, it will automatically create the table if not. TSK.
PRocedure TForm1.FormCreate(Sender: TObject
var
ap:TStringList; {Stringlist variable}
answer:Integer;
Begin
ap:=TStringlist.Create;
Session.GetAliasNames(ap); {get alias list}
if (ap.IndexOf(Cntssamp)=-1) then {Determine whether the alias exists}
Begin
answer:=application.MessageBox(the alias Cntssamp does not exist, is it created now?,BDE information window,mb—OKCancel);{Add a database alias named Cngzsamp}
if answer=IDCANCEL then
Begin
ap.Free;
Exit;
end;
Session.AddStandardAlias(Cntssamp,c:/delphp11,Paradox);
Session.SaveConfigFile; {BDE configuration file save}
end ;
ap.Clear; {get list of all table names in alias Cngzsamp}
Session.GetTableNames(Cntssamp,,False,False,ap);
if (ap.IndexOf(TSK)=-1) then {Determine whether the table exists}
Begin
answer:=Application.MessageBox (the table TSK does not exist in the alias Cntssamp, is it created now?, Table information window, mb—OKCancel);
if answer=IDCANCEL then
ap.Free;
Exit;
end;
with table1 do
Begin
Active:=false;
DatabaseName:=Cntssamp; {database alias}
TableName:=TSK; {Table name}
TableType:=ttParadox; {database type}
with FieldDefs do
begin {add field}
Clear;
Add(SH,ftString,30,False); {Book No. String(30)}
Add(SM,ftString,30,False); {Book title String(30)}
Add(CBS,ftString,20,False); {Publisher String(20)}
Add(CBRQ,ftDate,0,False); {published date Date}
Add(YS,ftInteger,0,False); {Page Integer}
end;
with IndexDefs do
begin {add index}
Clear; {Create the main index by book number field}
Add(SHSY,SH,[Primary,ixUnique]);
end;
CreateTable; {Create table}
end;
end ;
ap.free; {release variable ap}
end;