13.22Databases. 4tH 4tH Forth compiler
تبلیغات
تبلیغات
CHAPTER 13. SPECIAL LIBRARIES 233 or i8859htm.4th libraries. ”IS-HTML” works just like ”IS-XML”, but also reports a
HTML character entity when it’s not printable or simply outside the ASCII-7 range.
Like ”ASCII>XML” the ”CP437>HTML” word always returns a HTML character entity, even when there is no real equivalent to HTML. E.g. the first CP437 block character renders to ”°”. If you don’t want that you’ll either have to maintain an additional table or examine the character entity ”CP437>HTML” returns. Needless to say that
”ISO8859>HTML” works the same way. The only exception is the NULL character, which is not allowed at all. In that case an empty string is returned.
13.22
Databases
4tH also has a small database package which perfectly blends in with the language. First define a structure. That will be your database buffer: include lib/dbm.4th
struct
16 +field Firstname
32 +field Lastname end-struct /Person
/Person buffer: (Person)
Second, create a database file. You have to do this only once. If you do it again, it will create another empty database file, so take care!
s" Persons.dbm" db.create
Now add it to the data dictionary. All you have to do is to declare the buffer, its size and the database file. It will return a database handle.
(Person) /Person s" Persons.dbm" db.declare to Person
After that, you have to tell the datadictionary that you want to use it - or make it current, if you prefer:
Person db.use
Now we’re ready to add records. Simply clear the buffer, fill in the fields and insert the record. When you’re done, repeat the operation: db.clear
s" Hans" db.buffer -> Firstname place s" Bezemer" db.buffer -> Lastname place db.insert
db.clear
s" Chuck" db.buffer -> Firstname place s" Moore" db.buffer -> Lastname place db.insert
Finally, when you’re done simply shut it down:
CHAPTER 13. SPECIAL LIBRARIES 234 db.close
That’s all! If you want to use it again, simply define the buffer and declare it to the datadictionary: include lib/dbm.4th
struct
16 +field Firstname
32 +field Lastname end-struct /Person
/Person buffer: (Person)
(Person) /Person s" Persons.dbm" db.declare to Person
Of course you can add additional structures and files, the procedure remains the same. If you want to use a table, simply use it:
Person db.use
Since this is the first time you are accessing the table it is automatically positioned at the first (sequential) record. Just fetch the values: db.buffer -> Firstname count type space db.buffer -> Lastname count type cr db.buffer
always points to the current table. Of course, you can also address the buffer directly - that is up to you:
(Person) -> Firstname count type space
(Person) -> Lastname count type cr
If you explicitly want to go to and fetch the first sequential record, you can also issue: db.first
The next (undeleted) sequential record is reached by issuing: db.next
If you want to know which record you’re at, you can consult db.rowid: db.rowid . cr
This number is important, because it never changes and uniquely identifies a record. If you want to return to a specific record, you can by using this number:
1 db.goto
Note that whether you access the record random or sequentially, the buffer is always updated accordingly. You can also search for a specific string:
دانلود
تبلیغات