|
Adds a record to the end of the database that has alias db_alias and positions the record pointer at that record. If NAME is omitted, the record is added to the primary database.
Before <MvADD> is executed, each field for the record you want to insert must be assigned to database variables as shown below. <MvADD> locks the record that is being added. Attributes
ExamplesA database must be open before you can use it. When a database is created, it is opened automatically. The script that you used to create the database will usually be run only once, so before you can access the database, it has to be explicitly open using the <MvOPEN> tag. A database stays open until the script terminates, or until you close it with <MvCLOSE>. The next time you use the database, you have to open it again. Before executing <MvADD> database field variables must be explicitly assigned. Assigning must be done after the database is opened. Field variables take the form db_alias.d.fieldname where fieldname is the name of the field when it was created. .d. is a special prefix standing for 'database'. Example:<MvOPEN NAME="worker_db" DATABASE="dbs/workers.dbf" INDEXES="work_name.mvx,work_sal.mvx"> <MvASSIGN NAME="worker_db.d.employee" VALUE="Walter Skinner"> <MvASSIGN NAME="worker_db.d.title" VALUE="Assistant Director"> <MvASSIGN NAME="worker_db.d.salary" VALUE="150000"> <MvADD NAME="worker_db"> <MvCLOSE NAME="worker_db"> In this example, the database has fields called employee, title, and salary. The database may have other fields. If unassigned they will be contain a null value. The index files work_name.mvx and work_sal.mvx associated with the database are also updated when an <MvADD> is executed. The db_alias is optional for variables and <MvADD> whose values are to be stored in the primary database. For example, since worker_db was just opened, it is the primary database. The following block of code would have worked the same in the example above. Example:<MvASSIGN NAME="d.employee" VALUE="Walter Skinner"> <MvASSIGN NAME="d.title" VALUE="Assistant Director"> <MvASSIGN NAME="d.salary" VALUE="150000"> <MvADD> Variables of the form d.field_name will assign values to the primary database. In contrast, the variables db_alias.d.field_name will only assign values associated with the database with alias db_alias. In practice most programmers use the longer form of the variable name making their code more readable and easier to maintain. IMPORTANT: The same set of field variables is used for both reading and writing database records. The field variables automatically load the values of the current record and any variables that you do not explicitly assign, will retain the values for the current record. These will be written when the new record is added. For this reason, you should assign all relevant variables a value before using <MvADD>. Variables for fields to which you do not want to assign an explicit value at this time should be assigned null values. In this example the first database record is loaded with <MvGO>. All field variables are populated with data from the first record. Any field that is not explicitly assigned (or cleared) will be written to the new record by <MvADD> Example:<MvGO NAME="worker_db" ROW = "TOP"> <MvASSIGN NAME="d.employee" VALUE="Fox Mulder"> <MvASSIGN NAME="d.title" VALUE=""> <MvASSIGN NAME="d.salary" VALUE=""> <MvASSIGN NAME="d.started" VALUE=""> <MvADD> Date FieldsFor date field variables (See MvCREATE ) you assign a value to a date field as an 8 digit number formatted in year month day. (e.g. yyyymmdd ). No validation is done so it is up to you to confirm the value contains a valid date. Example: year month day<MvASSIGN NAME="worker_db.d.started" VALUE="{ '19990102' }"> i.e. Jan, 2, 1999 <MvADD NAME='worker_db'> If more precision is need, that is you also need to store the time, you can store the Unix style time_t value where time_t is defined as the number of seconds from midnight 01/01/1970. Miva Script supplies the current time in s.time_t and s.dyn_time_t. The function mktime_t() can be used to generate a specific time_t value, given the date, time, and timezone. In this example the started field was created as Started NUMBER(10.0). Example: time_t value<MvCOMMENT> Assign a value as a specific date and time </MvCOMMENT> <MvASSIGN NAME="worker_db.d.started" VALUE="{ mktime_t( 9999, 12, 02, 08, 15, 00, timezone() ) }"> i.e. Jan, 2, 1999 08:15:00 <MvADD NAME = "workers"> <MvCOMMENT> Assign the current server time. </MvCOMMENT> <MvASSIGN NAME="worker_db.d.started" VALUE="{ s.time_t }"> <MvADD NAME = "workers"> |