Dynamic establishment of Delphi database

xiaoxiao2021-03-06  104

One of Delphi's most attractive features is its powerful database access capabilities, which can be easily established by Database Desktop tools, editing databases. Because of actual reasons, we often need to dynamically establish a database in the program running. If you let users use the Database Desktop tool to create a data sheet, then what you write will make a discount, but you don't have to worry about Delphi to complete this feature using the language to provide us with convenience. I have summed up two ways in learning and practice, I am called Table method and SQL method. The process of establishing a dynamic database is described below by a simple example. 1. Table method: 1, (with the establishment of the Paradox data sheet as a library name to ljh.db). New project files zhouDf.dpr. DB, DBTABLES units are added to the USES statement in Unit1. 2. Select the Button element on the panel to place in the Form1 table, double-click the button1 to enter the following code. Procedure Tform1.Button2Click (Sender: Tobject); var table1: ttable; begin table1: = ttable.create (self); with table1 do begin active: = false; tablename: = 'ljh.db'; tabletype: = ttparadox; with Fielddefs do {This method is ljh.db add field} Begin Clear; Add ('yj', ftdate, 0, false); add ('zp', ftstring, 10, false); {Add specific field name, type} Add ('ZDM', FTINTEGER, 0, FALSE); End; With indexdefs do {This method is ljh.db Add index field} begin clear; add ('yjindex', 'yj', [ixprimary]); end; createtable ; End; end; two, SQL method: Select the Button component on the panel to place in the Form1 table, double-click the button to enter the following code. Procedure TForm1.Button2Click (Sender: Tobject); becom table2: tQuery; begin table2: = tQuery.create (self); with table2 do begin with sql do begin clear; add ('create table "ljh.db"); add ('(Yj Date,'); {Note '('} add ('zp char (10),'); add ('zdm int)'); {pay attention to ')'} end; Execsql; sql.clear; sql.add ('create index yj on "ljh.db"); {This SQL statement is ljh.db add index field} EXECSQL; END; END; * Compile this program . * Need to note that using the SQL method to build a library if the library has an error prompt, you don't have to consider using the TABLE method.

转载请注明原文地址:https://www.9cbs.com/read-96924.html

New Post(0)