Accessing Database Records

The currently selected database record can always be accessed through the %rec database variable. For example, #SHOWDB %rec will display the current record to the screen. All of the commands and functions in the previous section can be used on %rec. The %db function (and the dot notation %rec.FieldName) is specially optimized to return the value of the database field very quickly. Note that #DELKEY and %delkey do not function for %rec since you can't delete fields under program control.

To access a database record other than the current record, you specify the record number, appended to the database name. If you leave the database name out, the current database will be used. For example, to display record 20 in the current database, you can do

#SHOWDB 20

To show record 20 from the Equipment Name (name is eq), you would use

#SHOWDB 20eq

This syntax for record numbers can be used by in any of the database variable commands and functions described in the previous section (except for #DELKEY and %delkey). In fact, the combination of a database record number and a field using the dot notation can be used anywhere a normal zMUD variable name is used. For example,

#ADD 20.Hit 2

will add 2 to the Hit field of record 20 in the current database. The side effect of this is that you cannot use variable names that start with a number when also using the Database Module. Another, more complicated example:

#MATH 20.Hit @19.Hit*2

will set the Hit field of record 20 in the current database to the value of the Hit field in record 19 times two. Assignment statements also work with this notation:

20.Hit = 2

sets the Hit field of record 20 in the current database to 2. This tight integration of database variables with the zMUD parser allows for some very powerful scripts to be written.

To retrieve the entire database variable from a database record, use the %dbget function. The argument is the record number and optional database name. So, %dbget(20eq) will retrieve record 20 from the Equipment Database. The #DBGET command sets the current database variable (%rec) to the specified database record.

To set a specific record, use the #DBPUT command. The first parameter is the record number to save. If omitted, the data is stored to the current database record. The second argument is the database variable to be stored, or a list of keys and values to be changed. For example,

#DBPUT 20eq %dbget(21eq)

will store a copy of record 21 in the Equipment database on top of record 20. Actually, you can do the work of #DBPUT with #ADDKEY, but sometimes it is easier to read to use #DBPUT. For example, both of the following lines change the Name field of record 20 to Zugg:

#ADDKEY 20 Name Zugg
#DBPUT 20 Name Zugg

Actual database records also have an additional field name that is always present, called Num. Num returns the record number in the database with the two-character database name appended to it. So, in the above example, %db(,Num) would return 20eq.



Contents Database Variables Using Database Views