Using Director with Databases: Part 2
by Gary Rosenzweig
Last week I showed you how to make a simple database. This week, let's expand that project to include a way to save and load the database so that you don't need to start from scratch each time you run the program. I'll also show you how to export the information in the database in a format that can be read by other programs, like Microsoft Excel. While the program is running, the data is stored in a global called gDatabase. This is a list. It is actually a list of lists, with each sublist being a single record in the database. Here is how it looks in the Message window:
Director can't save lists directly into files, but it can convert the list to a string, and then save that. Converting to a string is as simple as using the string function. Here is the saveData function.
convert list to a string --
send string to text file end
convert list to a string
send string to text file
The saveText function is a standard one that I use for saving text files with the FileIO Xtra. It is from my Using Director 8 book (page 369). I also wrote about it in a previous article. I won't go into that code here, but you can find it in those two places, as well as the source file for this article.
Once you save the data, you should be able to look at it in SimpleText on the Mac and Notepad or Wordpad in Windows. It is just a straight representation of the list as a string.
Getting the data back into the program is only slightly more complex. For one thing, you want to take into account if the user presses the "Cancel" button when being asked for a file name. Also, if there is a problem with the file they select, you will want to tell them.
This is what the loadData function looks like. It uses the value function to attempt to convert the string to a list. It uses the listP function to determine if a list could be made.
get contents of text file --
if text file is empty, could mean the dialog was cancelled --
convert string to list --
did the conversion work? end
get contents of text file
if text file is empty, could mean the dialog was cancelled
convert string to list
did the conversion work?
There is a chance that the value function could fail, even though the user selected a valid file. This is because strings in Director can contain quote characters. So, if the user types in their name as "John "JS" Smith" then our code will save that out with all of the quotes involved: both the ones surrounding the name and the ones that are a part of the name. However, when the value function tries to interpret this, it will read all of the quotes as ones to interpret when rebuilding the list. This will create an error and the value function will give up, returning VOID as the value for the list.
There are several ways to avoid this. The one I like the most is to make sure that the input text members don't accept quotes at all. Or, they could accept quotes, but convert them to harmless single quotes. Another option is to convert the quotes in each item of the record to single quotes when it is first stored. You can find out more about limiting text input in a previous article.
The saveData and loadData functions provide a way for you to save and load the database. You can even save and load several different databases for whatever reason. However, these files cannot be read by another program. Lets create an export function that will save the data in a tab-delimiter format that can then be read by programs like Microsoft Excel.
The only trick here is to save each item in each record with a TAB character after it. You should also put a RETURN between records. The export function will even use the list properties to create a line of column titles. All of this is put into a text string. When the string is complete, it is saved with the same saveText function.
< 1 then
< 1 then
The resulting text file can be looked at with SimpleText or Notepad. It is a little easier to read than the list-formatted text file.
Name Street City State Zip Phone Birthday Gary 123 Clever Rd Denver CO
80210 720-555-1212 2/29/69
Programs like Excel allow you to open or import files and choose how the data should be read from those files. Usually this type of file will be called tab-delimited or something similar.
Note that there is no Shockwave version of this example movie. That is because it uses the FileIO Xtra, which does not come with Shockwave. Products like Shockwave need to be careful about what sort of things that the content can do. Imagine if Shockwave had the ability to create, modify, or even delete files off of the user's hard drive. Imagine the damage that a malicious Shockwave developer could do with that power. This is why Projectors have functions like FileIO that Shockwave does not. So download the example movie to try this one out.
Next week, we'll look at more ways to improve the database program.