Author: Thinking: In this lecture, we used Delphi5 as the development tool and the standard paradox table as the background database, to show you how to perform the simplest database programming. In this example, you can add, modify, delete, and query data in a table. The hard and soft computers used in this example: Thinking: Nanhai
Database programming example
In this lecture, we use Delphi5 as the development tool and the standard paradox table as the background database to introduce how to perform the simplest database programming. In this example, you can add, modify, delete, and query data in a table.
In this example, the computer hardware and software environments are Windows NT 4.0 Server, Borland Delphi 5, PIII550, and M memory. Of course, normal friends can compile programs in the Win98 environment or Win2000 environment according to the steps in this example. My machine is mainly used as a server, so I will compile it on the server. Start now:
A lot of preparation work is required before the program is officially written, including Borland Delphi 5 installation. Delphi5 comes with a drive for the standard data table paradox (compatible with some. db files of the fox database software, which can be directly read by Delphi ).
The first step in programming is to create a data table (the. db file. In this step, you need to use the Database Desktop tool that comes with Delphi. This tool is mainly used to operate a table. standard tables such as db files can also access files in large databases such as Oracle and SQL Server, which are very powerful. Open Database Desktop, select File-New-Table, and select paradox7. the following page is displayed:
Enter the attribute name of the form you want to create in FieldName. In this example, the personnel management table is created, therefore, the created attributes include number, name, gender, work unit, salary, and remarks. All attributes except salary are string types, that is, the (Alpha) selected for the Type item in the figure, while Size indicates the length of the string, the length of the attribute value is determined based on the specific meaning of the attribute. for example, the length of a Chinese character is "male" and "female" for "gender, you can set the length of the gender attribute to 2 (a Chinese character is equal to the length of two characters ). The wage property is of the numerical type and is marked with Number in paradox, that is, the "N" corresponding to the preceding one ". After setting the attributes, select Save As storage. In this example, the path and file name of the created data table are D: \ Program Files \ Borland \ Database Desktop \ WorkDir \ ryb. db (Personnel table ).
This completes the first step. This step is only preparations before programming, and is also the basis for writing database applications for this form. The following is the actual program. open Borland Delphi 5.
Place such controls on the default Form1 (you don't have to worry about the control's position, do you ?) : TdataBase, Ttable, TdataSourse, TdbGrid, Tlabel, TdbNavigator. Place these controls according to your preferred interface style, such as the controls in this example:
The next step is to set the control attributes. This step is also the most important step in this example, because many netizens are unfamiliar with the background database programming using the front-end development tools, that is, this place.
We set the DataBaseName attribute of Tdatabase to Mydb (this name can be set as needed), drivername to STANDARD (indicating that the driver is a paradox table), and params attribute to path = D: \ Program Files \ Borland \ Database Desktop \ WorkDir (storage. db file path, as mentioned earlier), connected is set to true (establish a connection with the data table), and other attributes can use the default value.
Set the Databasename attribute of Table to Mydb (that is, the DataBaseName attribute of Database1) and tablename to ryb. set the Active attribute of db (the name of the data table) to True (open the form to facilitate programming ).
Set the dataset attribute of eclipse1 to Table1 (name attribute value of Ttable ). In addition, both DBNavigator1 and DbGrid1 set their own se attributes to eclipse1. The caption attribute of Tlabel is set to "database programming example" (you can modify the font according to your preferences to make it look better ). > In this way, the properties of the control are set. Note that the "number" and "name" columns in dbgrid are all attributes in the data table. db, not in the front-end delphi program. Click the run button to run the simple program (see ). > You can use the buttons on Dbnavigator to add, delete, and modify the data in the form. (if you change the ShowHint attribute of DbNavigator to True, you can see the prompts of each button at runtime).
At this point, we have implemented the use of rapid development tools such as Delphi to write simple database applications. careful readers may have noticed that no program has been written so far. this is indeed the case, this is the benefit of the fourth generation programming language (visual programming language. Of course, it is impossible to write complex programs without writing code. next we will implement the query function on the basis of the above and attach the source program to you. Add the following controls to Form: One Tquery, one Tbutton, four Tlabel, and two Tedit. Modify the properties of Tlabel as shown in the following figure (the newly added controls are all in the black box ):
Set the DataBaseName attribute of Query1 to MyDb (the same as Table), write Select * from ryb to SQL, and set RequestLive to True (this allows you to modify the data in the Query ), set the Active attribute to True. Finally, change the DataSet attribute of eclipse1 to query1 (the Table1 control can be removed from Form ).
Double-click Button1 and write the following code in the program editor:
Query1.close;
Query1. SQL. clear;
Query1. SQL. Add (Select * from ryb );
Query1. SQL. add (where name like: xm and salary>: gz );
This completes the compilation of the query code. for the meanings of the functions referenced by each statement, refer to the help document of Delphi. In addition, when writing a query program, we will introduce some knowledge about database query language SQL. > Execute this program to program the database program with the query function. Enter the corresponding query conditions in Edit1 and Edit2, and click Button1 (OK). The program executes the corresponding query operation. the query results are displayed in DBGrid1.
So far, we have implemented a program example that uses Borland Delphi5 to manipulate the paradox data table. of course, this program is very simple and only implements the most basic functions of database operations. In addition, this program only runs in the click environment. during the design, we do not need to consider some issues such as concurrency. What is more mature now is to operate databases in the client/server structure and in the browser/server, and with the addition of network functions, many new problems are also brought in, we will give an example later. please wait for the sound!
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.