Articles Archive
Articles Search
Director Wiki
 

Using Director with Databases: Part 1

February 8, 2001
by Gary Rosenzweig

I want to start a short series about using Director as a database program. In this first installment, I'll show you how to let the user (or yourself) enter data into a small database. I'll also show you how to list the items in the database.

As an example, we'll just create a simple database of addresses. Keep in mind that the data itself is not important. You could alter this program or make your own to store any data you want.

We'll store the data in a list. There are two types of lists in Lingo: linear lists and property lists. A linear list is a simple list of items. We'll use this to store all of the data. Each person entered into the database will be an item in the linear list. Here is the handler that will initialize this list.

on startNewDatabase

  gDatabase = []

end

This first stage of our program will have two options: Add Entry and List Data. You can see them both as buttons in the Shockwave version of this movie near the end of this column. When you press Add Entry, you'll be taken to a screen with a whole bunch of text entry fields. Before we display this screen, lets clear all of the fields. Here is that handler:

-- clear entry fields and go to entry screen

on startNewEntry

  member ("entry-name").text = ""
  member ("entry-street").text = ""
  member ("entry-city").text = ""
  member ("entry-state").text = ""
  member ("entry-zip").text = ""
  member ("entry-phone").text = ""
  member ("entry-birthday").text = ""
  go to frame "New Entry"

end

Note that when I say "fields" I am not referring to the antique "field" cast member in Director. In this movie I am using regular text members set to be editable. These are far better than those old fields. For instance, the text they display is anti-aliased.

Now, on the entry screen, the user can tab between fields and enter data. No Lingo is involved here. However, you can attach some Lingo to limit what is entered. See my previous column "Limiting Text Input".

There are three buttons on this screen. One lets the user clear all of the fields. This will simply call on startNewEntry again. Another lets the user return to the main menu, throwing away anything they have typed. The last option will enter the data into the database. Here is the handler for that action:

-- get all fields of entry screen and add to database

on addNewEntry

  newEntry = [:]

  addProp newEntry, "Name", member ("entry-name").text
  addProp newEntry, "Street", member ("entry-street").text
  addProp newEntry, "City", member ("entry-city").text
  addProp newEntry, "State", member ("entry-state").text
  addProp newEntry, "Zip", member ("entry-zip").text
  addProp newEntry, "Phone", member ("entry-phone").text
  addProp newEntry, "Birthday", member ("entry-birthday").text

  add gDatabase, newEntry

  go to frame "main menu"

end

Each entry is its own property list. A property list is like a linear list in that it has item after item of data in it. However, it is different in that each item has a label. When you use addProp to add an item to a property list, you must include both the property name and the value. Then, when you wish to get the information out of the list, you can refer to it by its property name instead of just its position in a list. Here is what a sample property list would look like:

["Name": "Gary Rosenzweig", "Street": "123 Clever Rd.", "City": "Denver", "State": "CO", "Zip": "80210", "Phone": "720-555-1212", "Birthday": "2/29/69"]

After this property list has been compiled, it is added as an item to the gDatabase linear list. That's right: lists can be items inside of lists. The variable gDatabase is a global so its value is kept even when it is not being used in a handler.

So far, the program has the ability to let the user enter person after person into the database. To prove that the data is there, we'll use the List Data button on the main menu to show all of the entries in a scrolling text member. Since plain text does not handle columns of data very well, we'll use HTML code to make a simple HTML table of the data. Then, we'll set the html property of the text member to put the table into it. See my column "Table Manners" for more information about HTML in text members.

-- show entire database in HTML list

on listData

  if gDatabase.count < 1 then
    -- if no data yet, then show message instead
    member ("list").text = "No data."
    
  else
    -- start html code
    html = "<HTML><BODY><TABLE BORDER=1><TR>"
    
    -- create headings row
    repeat with i = 1 to gDatabase[1].count
      put "<TD>" & getPropAt (gDatabase[1],i) & "</TD>" after html
    end repeat
    put "</TR>" after html
    
    -- put each entry in a row
    repeat with i = 1 to gDatabase.count
      put "<TR>" after html
      
      -- put each item in a column
      repeat with j = 1 to gDatabase[i].count
        put "<TD>" & gDatabase[i][j] & "</TD>" after html
      end repeat
      put "</TR>" after html
    end repeat
    
    -- end html code
    put "</TABLE></BODY></HTML>" after html
    
    -- put html into member
    member ("list").html = html
  end if

  go to frame "list data"

end

Here is the program so far:

So far, we have the basics of a database program. In future columns, I'll show you how to add more features, like altering and deleting entries and saving to a file in a format that Excel can read.

A sample Director 8 movie is available for download in Mac or Windows format.

Gary Rosenzweig's two most recent books are: "Special Edition Using Director 8" -- The most comprehensive guide to Director ever, including tons of examples and demo movies. It's suitable for novices and experts alike. "Advanced Lingo for Games" -- How to make games with Director 7 and 8. This book comes complete with full source code for more than 20 complete games. More information about these books can be found at http://clevermedia.com/resources/bookstore/. They can be purchased there, or in your local bookstore.

Gary Rosenzweig is the Chief Engineer, founder, and owner of CleverMedia, a game and multimedia development company in Denver, Colorado. He is the author of ten books on Macromedia Director and Flash, including his latest, Special Edition Using Director MX.

Copyright 1997-2017, Director Online. Article content copyright by respective authors.