Source language: Translate to:

ID autonumeric field

Questions about using NeoBook's scripting language

Moderator: Neosoft Support

ID autonumeric field

Postby lesanchss » Mon Apr 09, 2007 9:45 pm

Hi friends,

I'm using a dbf table and i need some ID autonumeric field but dbf tables does not support this kind of propertys, so i'm working with this routing:

If "[agenda.ID]" "=" ""
SetVar "[agenda.ID]" "1"
ELSE
dbfAddRecord "[PubDir]agenda.dbf"
dbfGetRecordCount "[PubDir]agenda.dbf" "[num]"
Math "[num]+1" "0" "[agenda.ID]"
EndIf

but i recive errors because sometimes appear some blank records and them start to count again, besides if the table are empty the number 1 appear but later appear the number 3 instead 2 and continues 4,5,6....

Help please,
thanks.
Lesanch.
Todos somos ignorantes, solo que no todos ignoramos las mismas cosas.
Albert Einstein.
.
. .
User avatar
lesanchss
 
Posts: 24
Joined: Wed Sep 20, 2006 12:38 pm
Location: Cuba

Postby smartmedia » Tue Apr 10, 2007 1:28 am

Hi...

You can use the dbfMax command from DB plugin:
The only thing you must do is to change the id field as integer.

And use the dbfDefineAlias "[PubDir]agenda.dbf" "agenda" in the beginning.

Example:

Code: Select all
dbfShowAll "agenda"
dbfMax "agenda" "id" "[MaxNum]"
If "[agenda.id]" "=" ""
SetVar "[agenda.id]" "[MaxNum]+1"
else
dbfAddRecord "agenda"
.dbfGetRecordCount "agenda" "[num]"
SetVar "[agenda.id]" "[MaxNum]+1"
EndIf


This way every time you create a new record will take a new id based on biggest number exist allready.
Try it and tell me if has been worked for you.
Thanks
User avatar
smartmedia
 
Posts: 889
Joined: Fri Apr 01, 2005 6:50 am
Location: Hellas

Postby beno » Tue Apr 10, 2007 8:36 am

Hi,

Since it is possible that records may be deleted in your database it would be possible that you can generate the same ID using dbfMax on your database.

I use a integer field in another table to store this index (a separate table specially designed to store indexes). Each time I add a new record I update the field that belongs to that table.

Using this idea it is possible to add and delete records to your database and all IDs will always be unique.

HTH,

beno
User avatar
beno
 
Posts: 678
Joined: Fri Apr 01, 2005 9:03 am
Location: México

Postby Gaev » Tue Apr 10, 2007 9:53 am

beno:
Since it is possible that records may be deleted in your database it would be possible that you can generate the same ID using dbfMax on your database.

According to the Help file ...
Code: Select all
Calculate the maximum value of a single field from each record in the database. If a search query is active, only records matching the query will be examined. You could use this action to examine an inventory database to determine the most expensive item in stock.

So I don't see how deleting records will cause duplicates ... gaps yes, but not duplicates.

However, you have to make sure you invoke a dbfShowAll first ... so you get the maximum value of the field in ALL records ... not just the ones being queried right now.
User avatar
Gaev
 
Posts: 3718
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Postby beno » Tue Apr 10, 2007 3:39 pm

Hi,

So I don't see how deleting records will cause duplicates ... gaps yes, but not duplicates.


What happens if you delete the record with the maximum value in the ID field? (there is a chance this can happen).

Then, the next time you create a new record the algorithm may produce the same (gone) ID.

Let's suppose these IDs are stored in other tables with relations to other criteria. This is what we are looking for, since we are using this technique to play with NBDB as a relational database.

Now, following the last 2 paragraphs, there is a big chance that the new record points to the other tables and has inherited some relationships that does not belong to it.

The last idea can happen.

But Ok, if you are a cautious coder you can avoid this by deleting all the references of this record when you delete the record . But there is a chance that you forgot to do it or have no plans for this.

The idea I'm suggesting creates non repeating IDs. And works nice with relationships among different tables.

My cent,

beno
User avatar
beno
 
Posts: 678
Joined: Fri Apr 01, 2005 9:03 am
Location: México

Postby lesanchss » Wed Apr 11, 2007 6:46 am

Thanks All Friends.
Todos somos ignorantes, solo que no todos ignoramos las mismas cosas.
Albert Einstein.
.
. .
User avatar
lesanchss
 
Posts: 24
Joined: Wed Sep 20, 2006 12:38 pm
Location: Cuba


Return to NeoBook Action Commands

Who is online

Users browsing this forum: No registered users and 1 guest