Skip to content

MvCREATE#


Creates a new database with the structure specified by fields. Field types are CHAR, NUMBER, DATE, BOOL, and MEMO.

Syntax
<MvCREATE NAME = "string: {  expression } | literal"
          DATABASE = "string: {  expression } | literal"
          FIELDS = "{ expression } | variable list"
          TYPE = "xbase3 | user defined"> 

A database must be created before it can be used. When you create a database, you are defining its structure. An empty database file is created on your system. This step needs to be done only once for each database.

If the database file already exists, it will be overwritten with an empty database with the specified structure. When a database is created it is implicitly opened, just as with <MvOPEN>, with the alias in NAME. Alias names can not contain the . (period) character.

Attributes#

Attribute Description
NAME Required. Database alias. When a database is open, it is referenced in all other database commands by this alias, allowing the same file to be opened with multiple aliases (often with different indexes).
DATABASE The physical path and filename of the database to be created. For xBase3 files (default) the extension is normally .dbf. If a MEMO field is specified, a second file with extension .dbt is created.
FIELDS A comma-separated list of field definitions:
- <field_name> CHAR(<max_chars)>: specify up to 254 characters (use MEMO for larger data).
- <field_name> NUMBER(<digits_before.digits_after)>: specify digits before and after the decimal (defaults to NUMBER(19) if omitted). Values exceeding precision are truncated or rejected.
- <field_name> DATE: fixed length YYYYMMDD.
- <field_name> BOOL: 1 = true, 0 = false.
- <field_name> MEMO: dynamic-length fields allocated as needed.
TYPE Optional. xBase3 (default) or MivaSQL. ODBC support has been dropped; omit for xBase3.

Sample Database#

<MvCREATE NAME="employees"
          DATABASE="{ g.path $ 'workers.dbf' }"
          FIELDS="
              name CHAR(40),
              title CHAR(20),
              salary NUMBER(7.2),
              started DATE,
              fulltime BOOL,
              comments MEMO">

Database Aliases#

The NAME attribute refers to a database alias. In MivaScript, you always reference databases by alias rather than physical filename. An alias is simply the name you assign when creating or opening a database. The same file can be open under multiple aliases (for example, with different indexes).

Database Files#

The DATABASE attribute specifies the new database’s filename, relative to the data directory. If creating in a subdirectory, create that directory first (e.g. with fmkdir()). You may use any extension, but .dbf is recommended. MEMO fields store data in a companion .dbt file—do not delete it.

Example#

Create or Open a Database
<MIVA STANDARDOUTPUTLEVEL="html, text, compresswhitespace">

<MvASSIGN NAME="g.path" VALUE="/myfiles/">
<MvIF EXPR="{ NOT fexists(g.path) }">
    <MvASSIGN NAME="g.ok" VALUE="{ fmkdir(g.path) }">
    Folder was created.<br>
</MvIF>

<MvIF EXPR="{ fexists(g.path $ 'workers.dbf') }">
    <MvOPEN NAME="employees" DATABASE="{ g.path $ 'workers.dbf' }">
    File is open.<br>
<MvELSE>
    <MvCREATE NAME="employees"
        DATABASE="{ g.path $ 'workers.dbf' }"
        FIELDS="
            name CHAR(40),
            title CHAR(20),
            salary NUMBER(7.2),
            started DATE,
            fulltime BOOL,
            comments MEMO">
    File was created.<br>
</MvIF>

Database Fields#

The FIELDS attribute defines each field’s name, type, and parameters. Field names may be up to 10 characters long. Data types:

  • CHAR(n): fixed-length text, up to 254 characters.
  • NUMBER(n.m): numeric with n digits before and m after the decimal (default NUMBER(19)).
  • DATE: YYYYMMDD format.
  • BOOL: 0 (false) or 1 (true).
  • MEMO: dynamic-length text stored in .dbt file.