Example of using database commands/functions

You could put the following in the Behavior Table as an action in response to Event=AVATARADD, to keep track of visitors and when they were last in your world. This example assumes the use of the example database, mydatabase.mdb, included in the Magsbot package. The part colored brown is optional; if not used, then the default connection specified on the Database window will be used. (This is new with version 2.1.4.)

@h=@dbopen["Microsoft.Jet.OLEDB.4.0","MyDatabase.mdb;Persist Security Info=False","MyTable"];
DATASET @h {
 DBTABLE MyTable;
 DBACTIVE 1;
 IF @dbfindfirst[$fmt["Name='%s'",$dbq[$avname]]] {
  REPORT "updating record" }
 ELSE {
  REPORT "creating new record";
  DBAPPEND };
 DBEDIT;

  DBFIELD Name=$avname;
 DBFIELD_ ID=@atr[AVATAR_CITIZEN];
 DBFIELD_ LastVisit=@now;
 DBPOST };
DBCLOSE @h;
 


Here's another method, using an SQL statement instead:

@h=@dbopen["Microsoft.Jet.OLEDB.4.0","MyDatabase.mdb;Persist Security Info=False","MyTable"];
DATASET @h {
 DBSQLSET $fmt["select * from MyTable where name='%s'",$dbq[$avname]];
 DBACTIVE 1;
 IF @dbreccount {
    REPORT "updating record";
  DBFIRST
}
 ELSE {
  REPORT "creating new record";
  DBAPPEND };
 DBEDIT;
  DBFIELD Name=$avname;
 DBFIELD_ ID=@atr[AVATAR_CITIZEN];
 DBFIELD_ LastVisit=@now;
 DBPOST };
DBCLOSE @h;
 

Note: the $dbq function used above is a string function that replaces single quotes with a pair of single quotes. The reason for this is that SQL would be confused by a single quote in the SELECT statement. For example, if a person's name was I'm Dangerous then you would get this SQL statement, which would fail, because SQL would think that the single quote in the name was a closing quote in the SQL statement itself:

    SELECT * FROM MyTable WHERE NAME='I'm Dangerous'

Using the $dbq function results in this statement instead, which is valid. because SQL recognizes a pair of single quotes as representing an actual quote character within the quoted string:

    SELECT * FROM MyTable WHERE NAME='I''m Dangerous'