[prev in list] [next in list] [prev in thread] [next in thread]
List: nsbasic-palm
Subject: Re: [nsbasic-palm] Keyed DB locate by approximate key?
From: Michael Verive <mverive () peoplepc ! com>
Date: 2001-12-30 2:41:06
Message-ID: 20011230024106.25427.cpmta () c002 ! snv ! cp ! net
[Download RAW message or body]
If the search key is greater than the last record, then dbRead will return 1 \
(operation failed). If you want to get to the last record in any database, use \
dbGetNoRecs to find the number of records, dbPosition to position the record pointer \
to that record, then dbGet to read the first field from that record. Use dbRead with \
that key to get a successful read, then use dbReadPrev as needed to read backwards \
through the data.
There's some confusion about NSBasic databases. You'll often see the terms "keyed" \
or "non-keyed" referring to databases. It's not the database that's keyed or \
non-keyed, but the manner in which the databas is accessed. Any NSBasic database can \
be accessed using non-keyed methods, simply by using dbPosition to tell NSBasic which \
record you want (and an optional offset, or number of bytes to skip to get to a \
specific field), then dbPut to put the data in a record, or dbGet to retrieve the \
data.
Non-keyed access methods are very simple to use, but not very powerful. You can't \
delete records, insert records between existing records, and you have to sort the \
data yourself if you want to keep it in order.
Keyed access methods allow you to read and write data as well, but also to update \
records, insert records, and delete records. Databases written in NSBasic using \
keyed methods are maintained in ascending, key-sorted order automatically. However, \
not all databases are suitable for keyed access.
To be accessible using all of the keyed methods, databases must follow these rules:
1. The records must be in ascending order by their first fields (which are the keys, \
by default) 2. The keys must be all the same data type (integer, float, string, etc) \
but cannot be user-defined types or arrays 3. The keys must be unique - no \
duplicates allowed
Any database created by an NSBasic app using keyed methods will adhere to these rules \
(it must - NSBasic won't allow you to break these rules!). However, if you start to \
write to a database using non-keyed methods it's easy to write records out of order, \
making keyed access unpredictable (keyed access relies on a binary search, which \
requires all records to be in sorted order).
Well, this was a long answer to a short question, but I wanted to get you on the \
right track from the start. It's usually easier to prevent problems than have to \
solve them!
Mike Verive
mverive@peoplepc.com
On Sat, 29 December 2001, "Roger Vicker, CCP" wrote:
>
> Michael,
>
> That's what I was looking for. The DB will be built by the user in the app so
> it will be in order.
>
> Question 2: If the search key is greater than the last record (in which case I
> would need to read backwards) where is the file positioned?
>
> Thanks.
>
> Roger Vicker, CCP
>
> Michael Verive wrote:
>
> > All you need to do is use dbRead to read the record by key. If the read is
> > successful, the result will be 0, and you will retrieve the data. If the
> > result is 2, then dbRead did not find the desired record, but the one that
> > immediately follows it, and has read the data for you.
> >
> > Just be sure that your records are all in key-sorted order, and the first
> > fields (which are used as the keys) are unique and of the same data type.
> >
> > Mike Verive
> > mverive@peoplepc.com
> >
> > ----- Original Message -----
> > From: "rvicker" <rvicker@vicker.com>
> > To: <nsbasic-palm@yahoogroups.com>
> > Sent: Saturday, December 29, 2001 3:25 PM
> > Subject: [nsbasic-palm] Keyed DB locate by approximate key?
> >
> > > Hello,
> > >
> > > Just getting started with NSBasic and am starting to get the hang of
> > > it (except conduits which will come later) but have one question.
> > >
> > > How do you position a keyed DB when you do not know for sure the key
> > > exists but want to start there (or next higher) and read by key order?
> > >
> > > An example would be a list of events by date and the user want to find
> > > events starting on or after an entered date.
> > >
> > > Thanks.
> > >
> > > Roger Vicker, CCP
> > >
> > >
> >
>
> --
> *** Vicker Programming and Service *** Have bits will byte *** www.vicker.com
> ***
> It is better to burn out than to fade away.
>
>
>
>
> Community email addresses:
> Post message: nsbasic-palm@yahoogroups.com
> Subscribe: nsbasic-palm-subscribe@yahoogroups.com
> Unsubscribe: nsbasic-palm-unsubscribe@yahoogroups.com
> List owner: nsbasic-palm-owner@yahoogroups.com
>
> Shortcut URL to this page:
> http://groups.yahoo.com/group/nsbasic-palm
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
Michael Verive
mverive@peoplepc.com
________________________________________________
PeoplePC: It's for people. And it's just smart.
http://www.peoplepc.com
Community email addresses:
Post message: nsbasic-palm@yahoogroups.com
Subscribe: nsbasic-palm-subscribe@yahoogroups.com
Unsubscribe: nsbasic-palm-unsubscribe@yahoogroups.com
List owner: nsbasic-palm-owner@yahoogroups.com
Shortcut URL to this page:
http://groups.yahoo.com/group/nsbasic-palm
Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic