Creating Databases

The easiest way to create a new database is to use FoxPro to define both databases and index files.

You can also program database creation. To make it possible for your project to create new database files, you must:

We'll discuss these tasks in reverse order because that's the easiest way to understand them.

Executing the DBCreate Function

The DBCreate function requires four parameters. In order, they are:

All of these parameters are self-explanatory except the last. If the fourth argument passed to the DBCreate function is "true," and FileFlex finds an existing file, it will not overwrite it. Instead, it will abort the database creation process and return an error. If the fourth argument is "false," it will overwrite any existing database file of the same name. Note that FileFlex appends a suffix, .DBF, to the name you provide, so it won't overwrite a text or other file of the same name, only a file whose name matches the name you provide as a first argument, with the .DBF appended.

After the database is created, it is automatically opened by FileFlex and is ready to receive data. If you don't want to work with the file immediately, be sure to close it.

If you include any memo ("M") fields, FileFlex will automatically generate a matching file with an extension of .FPT to hold the memo field information.

Automatic Databases

The DBCreateAuto is a more advanced version of the DBCreate function. DBCreateAuto will create an automatic or "structural" database, a FoxPro term. What this really means is that if you call DBCreateAuto, you're really creating not only the database file, but the index as well. The index that's created with DBCreateAuto is opened at the same time as the data file.

If you use DBCreateAuto to create a database called DATA that has both an index and at least one memo field, the following files would be created: DATA.DBF (the data itself), DATA.FPT (the memo file), and DATA.CDX (the index file).

Remember that FileFlex 3 now supports multiple, compound index sort orders in a given file, so you can create an automatic index that contains many different sort orders and selections. When you open the automatic database, you'd gain access to all these sort orders as well.

Describing the Database

The container in which you store the description of the database file to be created should be a field, a variable, or any other easily accessible container. The container should contain one line for each field to be created. Each line should contain, in order, these items, separated by commas:

Remember the xBASE field naming rules when naming fields in your database file: keep the names under ten characters, do not include anything but letters, numbers, and the underscore character. If you are unfamiliar with these rules, consult a good book on FoxPro. The field type will be one of these characters: C (for a character, or string, field), N (for a numeric field), M (for a memo field), L (for a logical, or Boolean, field), or D (for a date field), B (for binary), F (for float), and G (for general).

Character and numeric require lengths to be supplied. Only numeric, float, and general fields require the fourth element. For memo, Boolean, and date fields, no length information is required. Supplying it may produce unexpected results in the database file.

Here is an example of a database file description that might appear in a container:


Executing DBCreate with this description would produce a database file with six fields: two 24-character string fields; a two-digit, no-decimals age field; a logical field called MARRIED; a date field holding the date of birth and called DOB; and a memo field named COMMENTS.


If we wanted to create a database file called FRIENDS.DBF in the current default path using the fields in the previous section which we had stored in a card field called dbFields, this is how we would write the function call:

put dbCreate ("FRIENDS",6,cd fld dbFields,false) into dbResult

Discuss this chapter on the FileFlex Boards.

Copyright © 1992-1998 by David Gewirtz, under license to Component Enterprises, Inc.

Casa de Bender