Getting Started with the database series-Tip 4

Source: Internet
Author: User
Tags definition functions implement query
Skills | data | Database Author: Thinking about the finishing: Nanhai

Database Programming Examples



In this lecture, we take the Delphi5 as the development tool, the standard Paradox table is the backstage database, introduces how to carry on the simplest database programming. In this case, we'll implement the ability to add, modify, delete, and query data in a table.

The computer hardware and software environment used in this example is: Windows NT 4.0 server,borland Delphi 5,piii550,256m memory. Of course, the general friends in the WIN98 environment or Win2000 environment can be programmed according to the steps of this example. The author's machine is mainly used as a server, so it's on the server. Cut the crap and start now:

There is a lot of preparation to be done before the program is formally written, which of course includes the installation of Borland Delphi 5. Delphi5 itself with the driver of the standard datasheet paradox (and some of the Fox series database software's. db files are compatible, and these files can be read directly by Delphi).

The first step in writing a program is the establishment of a data table (that is, a. db file). In this step to use the Delphi with a tool called Database Desktop. This tool is primarily used to manipulate tables, not only to manipulate standard tables like. db files, but also to access files in large databases such as Oracle and SQL Server, which can be very powerful. Open the database Desktop, select File-new-table, then select Paradox7, and the following interface appears:

    

In FieldName, fill in the name of the form you want to create, as shown in this example, the establishment of the Personnel Management table, so the established attributes are numbered, name, gender, work units, wages, notes and so on. Attributes other than wages are string types, a (Alpha) selected by the type item in the diagram, and size refers to the length of the string, and the length of the attribute value is determined by the specific meaning of the attribute, such as "sex" only to choose "male" and "female", the length of a Chinese character, Then the length of the sex attribute is set to 2 (one character is equal to two characters in length). The wage attribute is a numeric type, marked with number in the paradox, which corresponds to the previous "N". When the property is set, select Save As disk. In this example, the data table that is established is stored in the path and the file name is: D:\Program files\borland\database desktop\workdir\ryb.db (Person table).

This completes the first step, which is just the preparation before programming and the basis for writing a database application for this form. The following to be done is the actual program written, please open Borland Delphi 5.

Place some of these controls on the default Form1 (the position of the control) I don't want to bother you anymore? ): Tdatabase, Ttable, Tdatasourse, TdbGrid, Tlabel, Tdbnavigator. Place the controls according to your preferred interface style, as shown in this example:

    

The next step is the control property setting, which is also the most critical step in this example, because many netizens are unfamiliar with the background database programming using front-end development tools, that is, in this place.

We set the tdatabase databasename attribute to MyDB (this name can be casually), drivername to standard (indicating that the driver is the Paradox table), params property is Path=d:\program Files\borland\database Desktop\workdir (the path to the. db file, as mentioned earlier), the connected is set to True (and the data table establishes a connection), and other properties use the default values.

The DatabaseName property of the table is then set to MyDB (that is, the DatabaseName property of the Database1), and the TableName (that is, the name of the data table) is set to True (the form is opened for easy programming).

The Datasourse1 DataSet property is then set to Table1 (Ttable's Name property value). In addition, the Datasourse properties of DBNavigator1 and DbGrid1 are set to DATASOURSE1.   The Tlabel Caption property is set to "database programming Example", as shown in the figure (you can modify the font to make it look better depending on your preferences). > So the control's properties are set, and notice that DBGrid inside the "number", "name" and other columns are the attributes of the datasheet, in the RYB.DB definition, rather than in the front of the Delphi program definition.   After clicking the Run button, the simple program runs (see figure below). > This is through the button on the Dbnavigator can control the form of the data added, deleted, modified (the Dbnavigator Showhint property to True if you can see the various buttons at run time prompts).

At this point, we have implemented a rapid development tool using Delphi to write simple database applications, careful readers may notice that so far not even a single program has been written, it is true, this is the fourth generation of programming language (visual programming language) to bring us benefits. Of course, to write more complex program does not write code is not possible, the following we will be based on the above to implement the function of the query, and to attach the source program. Add the following controls to the form: one tquery, one TButton, 4 Tlabel, 2 tedit. The Tlabel properties are modified as shown in the following illustration (the newly added controls are in a black box):

Set the DatabaseName property of Query1 to MyDB (same as table), write select* from ryb,requestlive to True in SQL properties (this allows you to modify the data in query), Then set the active property to true. Finally, the DataSourse1 dataset property is changed to Query1 (at which point the Table1 control can be dropped from the form).

Double-click Button1 to 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 Payroll >: GZ);

Query1.parambyname (XM). asstring:=%+edit1.text+%;

If (edit2.text<> ') then

Query1.parambyname (GZ). Value:=strtofloat (Edit2.text);

Query1.prepare;

Query1.open; >
  
This completes the query code writing, the specific statements referred to the function of the meaning of everyone can refer to the Delphi Help document, this will not be repeat.   In addition, when writing a query program, involves some knowledge of database query language SQL, in the following article we will have a more detailed introduction. > Execute this program at this time, programming the database program with query function. Enter the corresponding query condition in Edit1 and Edit2, then click Button1 (OK), the program executes the corresponding query operation, the result of the query will be displayed in DBGRID1.

So far, we've implemented an example of a program that uses Borland Delphi5 to manipulate the Paradox datasheet, and of course, the program is very simple, and it only implements the most basic functions of database operations. And this program is only in the context of the click Environment, in the design, we do not need to consider some of the problems such as concurrency. Now more mature is in the client/server structure and browser/server under the operation of the database, and with the addition of network functions, many new problems also brought in, in the back we will give this example, please wait for the good news!


Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.