Read-only archive of the All About Symbian forum (2001–2013) · About this archive

Small Database needed

1 replies · 1,855 views · Started 21 February 2005

I want to code a small database, but I dont know how. For example I have to store 3 values in my database: Name, Street and Town.

How can I save this 3 values and how can I show it in a table and edit it ?

Roman wrote:I want to code a small database, but I dont know how. For example I have to store 3 values in my database: Name, Street and Town.

How can I save this 3 values and how can I show it in a table and edit it ?

OPL has built in support for databases. Check out the CREATE, OPEN, INSERT, MODIFY, DELETE and PUT commands. An example:

Create a database with filename "FileName$" (the extra quotes are only for allowing filenames with spaces in them). The database contains one table "Addresses" with three string fields (Name, Street and Town). The numbers in brackets are the maximum size of the string fields (in this example Name can only be 40 characters long) and have to be between 1 and 255. For non-string fields (like the Number% field) you don't have to (can't) specify a size.


CREATE """" + FileName$ + """ FIELDS Name(40),Street(100),Number,Town(60) TO Addresses",A,Name$,Street$,Number%,Town$
CLOSE REM Closes the database

To open this database you can use OPEN:


OPEN """" + FileName$ + """ SELECT * FROM Addresses",A,Name$,Street$,Number,Town$

To open the database in read-only mode, use OPENR instead of OPEN.

After this you can access the database using the variable A (specified in the CREATE, OPEN or OPENR command, you can use A through Z), for example for inserting records:


USE A REM Makes A the active database
INSERT REM Insert a new record
A.Name$="Roman Keller"
A.Street$="Some street"
A.Number%=83
A.Town$="Hometown"
PUT REM Save the record

You always work on a current record. If there is no current record (for example after creating a database or deleting the current record), the EOF function will return KTrue%.

After opening a table, the first record is the current record. You can navigate through the records using FIRST, NEXT and PREVIOUS. The following code prints the contents of all records:


USE A
FIRST
WHILE NOT EOF
PRINT "Name: "+A.Name$+", Street: "+A.Street$+", Town: "+A.Town$
NEXT
ENDWH

To find a specific record, you can use FIND or FINDFIELD. To delete records use ERASE. If you need to do a lot of inserting at once, you can use BEGINTRANS and COMMITTRANS. POS returns the position of the current record (starting at 1) and COUNT returns the number of records in the table.

This is the basic stuff. You can find detailed explanations of all the commands in the OPL Wiki (the link is in the sticky first post in this OPL forum section).

Note that this is only the database interface which allows you to work with records. All the fancy user interface needed for editing, adding, deleting records etc, you still have to create yourself.

Hope this gets you started.