rebol_mini_text_thumbSometimes you just need a simple text database on your small Pocket PC or eeePC (either Windows or Linux as Rebol is fully multi-platforms) or don’t want to install a full-blown database like MySQL (see How to connect to MySQL with Rebol). Rebol’s blocks can be easily persisted on disk file and retrieved back with a few lines of code as explained in this tutorial here.

Let’s play with this to really understand how it works. Next installment, we will also create a visual GUI for this text database (for this part you can just use your favorite Notepad to edit your text database).

Launch Rebol’s console and type this:


record-1: ["John" "Doe" "email hidden; JavaScript is required"]
record-2: ["Jane" "Doe" "email hidden; JavaScript is required"]

We have 2 records. Let’s add them to a database container (db block):


db: []; create the block
append db record-1
append db record-2


We cannot distinguish between record-1 and record-2 any more. We have to use the mold function so as records will be added as a block to the db container:

mold converts a value to a REBOL-readable string.


db: []; create the block
append db mold record-1
append db mold record-2

We have 2 strings instead of 2 blocks inside the parent block, but we’re almost there, we just have to convert the string to a block with the to-block function:


db: []; create the block
append db to-block mold record-1
append db to-block mold record-2

You may ask why do we need the mold function to first convert into string before reconverting to a block ? Because to-block doesn’t do anything if the variable is already of block-type which was the case for record-1 and record-2.

Update July 5th: a more knowledgeable reader than me just told me I could just use append/only to achieve the same result so the code above can be simplified into:


db: []; create the block
append/only db record-1
append/only db record-2

If you forget the syntax one day, I have written a Rebol’s Style Cheatsheet.

Let’s now try to save all db records on disk’s file %db.txt:


delete %db.txt ; delete the file if it ever exists
write/lines/append %db.txt db/1
write/lines/append %db.txt db/2

This is how db.txt looks like:

Let’s read back the db.txt file into memory:


db: read/lines %db.txt
probe db

db variable will contain this block:


["John" "Doe" "email hidden; JavaScript is required" "Jane" "Doe" "email hidden; JavaScript is required"]

Not bad but we would prefer this:


[["John" "Doe" "email hidden; JavaScript is required"] ["Jane" "Doe" "email hidden; JavaScript is required"]]

So let’s try a new db format:


delete %db.txt
write/lines/append %db.txt mold db/1
write/lines/append %db.txt mold db/2

This time %db.txt will contain what we expect:


["John" "Doe" "email hidden; JavaScript is required"]
["Jane" "Doe" "email hidden; JavaScript is required"]

To load the data in memory, let’s try


db: read/lines %db.txt

db will contain:


[{["John" "Doe" "email hidden; JavaScript is required"]} {["Jane" "Doe" "email hidden; JavaScript is required"]}]

A block of 2 strings (strings in Rebol can be delimited by quotes or by braces), not exactly what we want.

To get a block of 2 blocks, we must use the load function:

Load reads and converts external data, including programs, data structures, images, and sounds into memory storage objects that can be directly accessed and manipulated by programs.


db: load %db.txt

db will then contain 2 blocks inside a block:


[["John" "Doe" "email hidden; JavaScript is required"]["Jane" "Doe" "email hidden; JavaScript is required"]]

In conclusion for this part, to save the whole database, we will just have to iterate through the db block:


delete %db.txt foreach record db [write/lines/append %db.txt mold record]

If you want to add a search function or look for more complete implementation, see links in References section.

Next Installment, we will bind this Text Database with a Visual Form and add the CRUD (Create, Read, Update and Delete) Database Operations.

References:

Bookmark and Share

Recent Articles