• Aucun résultat trouvé

Creating a Connection to a MySQL Database

Dans le document The Definitive Guide to MySQL5 (Page 161-165)

To open a new database project, execute FILE| NEW| DATABASEand select the option CONNECT TO AN EXISTING DATABASEand the database type MYSQL. In the next step you have the choice between ODBC and JDBC. If you wish to use Connector/J, as described in the previous section, choose JDBC.

In the third step you can check with the button TESTCLASS(see Figure 7-8) whether the instal-lation of Connector/J was successful. If that is the case, provide the name of the database and the computer name of the MySQL server (usually localhost).

Figure 7-7.Determining the Java installation path with OpenOffice

In step four you need to give the MySQL user name. If this user is secured with a password (which is generally the case), then activate the option PASSWORD REQUIRED. Now you can check with the button TEST CONNECTIONwhether all went well. You will be asked for the password.

Before ending the DATABASEWIZARDyou need to give a file name. In the Base file, first only the connection parameters are stored, then later queries, forms, and reports. The actual data remain on the MySQL server. The Base file thus contains not the content of the database, but simply objects that enable access to and management of the database. For this reason, the Base file is generally quite small.

Now the database component Base appears. This gives, in basic view, an overview of four modules:

TABLES, QUERIES, FORMS, and REPORTS(see Figure 7-9). At first, the existing tables in the MySQL database are available. Queries and other database objects have to be created. A preview of the currently selected table can be obtained by setting the listbox at the lower right in the window to DOCUMENT.

C H A P T E R 7 ■ M I C R O S O F T O F F I C E , O P E N O F F I C E / S TA R O F F I C E 129

Figure 7-8.Creating a connection to a MySQL database

Tables

The module TABLESdisplays a list of all existing tables and views. You now have various options, which are briefly described here:

Open a table: A double click on the table name opens the table view. You can browse through the table, and add, change, and delete records (see Figure 7-10). You can also copy or move via drag&drop rows that you have selected with Ctrl or Shift into other OpenOffice/StarOffice components.

Create a new table: You can create new tables either with a simple table editor (OpenOffice calls this the DESIGN VIEW) or with a wizard. The wizard offers a number of predefined table layouts and can save you time if one of the model tables suits your requirements.

Change the table layout: The context menu EDITleads into the DESIGN VIEWof an existing table.

There you can add, edit, or delete columns; define or delete indexes; etc. Note, however, that not all functions offered by MySQL are available. Since Base is also compatible with other data-base systems, the editing possibilities are reduced to the lowest common denominator.

Copy data to the clipboard: The context menu command COPYcopies all records of the current table to the clipboard. You can then insert the data into other OpenOffice/StarOffice compo-nents or into other text editors (format RTF or HTML).

Create a new view: This point leads to the query editor described later, in which you can create an SQL query. On the basis of this query a new view is created and stored in the database (see also Chapter 8).

Delete tables or views: You can also delete database objects via the context menu.

Figure 7-9.The OpenOffice/StarOffice database component Base

Caution

All operations change the MySQL database directly, not just some local copy of the data. If you delete a table in Base, it is irretrievably lost.

Queries

The module QUERIESis the most interesting part of Base. Here you can formulate queries simply and conveniently. This is particularly helpful to SQL beginners. You can create queries either in DESIGN

view, with a wizard, or in SQL view. The wizard, however, is useful only for queries that process data in only one table. In SQL view you must input the SQL code directly, which you can do just as well without Base. Therefore, we will concentrate on the query editor in DESIGNview, which looks much like the program MS Query introduced earlier in this chapter.

In the query design window you add with a double click all the tables whose data you wish to edit. The next step is to link fields between the tables using drag&drop. For example, in Figure 7-11, the publishers and titles tables are linked by the publID field. For Base to execute this link, move the publID field from the publishers table to the publID field of the titles table. (You can also use the reverse order, from titles to publishers.)

By default, Base creates so-called INNER JOINS. This means that in linking, only those fields that appear in both tables are considered. If NULL fields are to be allowed in one table, then you need a LEFT or RIGHT JOIN. The relational properties can be set by double clicking on the linking line. (More on the JOIN variants appears in Chapter 9.)

Tip

For Base to be able to generate the correct SQL code for MySQL for links among several tables, you must deactivate the option Use the SYNTAX{OJ} FOROUTERJOINS INEDIT| DATABASE| ADVANCEDSETTINGS.

C H A P T E R 7 ■ M I C R O S O F T O F F I C E , O P E N O F F I C E / S TA R O F F I C E 131

Figure 7-10.Viewing and editing a table with Base

After these preparations, you simply need to select the desired result columns with a double click, provide a label (row ALIAS), and if necessary set the sort order and grouping and aggregate functions. In the query shown in Figure 7-11, the result is grouped by title. For every such group, the number of authors is calculated. In the result, only titles with more than two authors will be displayed.

A click on the button EXECUTEQUERYreturns the result of the query. If you wish to know what the SQL code of your query looks like, you can toggle between design and SQL modes with the DESIGN VIEWbutton.

Dans le document The Definitive Guide to MySQL5 (Page 161-165)

Documents relatifs