Thanks Brad!
That was helpful.
I did some reading in the good ol' RB online manual and it turns out
that they've solved this problem in version 5.5
The REALdatabase class has a method called 'LastRowID' which, when
combined with the fact that it automatically inserts a hidden column
called, '_rowid', returns a unique identifier to the last row in the
table. Each new record is subsequently numbered automatically as well,
solving this problem and adding a really useful feature!
Hope this helps someone out there like me. :-)
Cheers,
Kristin
PS: As I wrote this, I see that Joe has already offered this
suggestion. Thanks guys!
On Jun 30, 2004, at 12:29 PM, Brad Rhine wrote:
While we're on the topic, how would one implement 'auto-increment'
and ensure that the field is unique?
I use a method like this. Note that myDatabase is a global reference
to the database I'm using. This particular function is used with a
SQLite database, but it should work with the REAL Database with
minimal conversion. This code selects the integer column you want from
the table you want and returns the maximum value plus one.
Function getNextID(table as string, keyfield as string) as Integer
dim rs as recordSet
dim sql as string
dim i as integer
if myDatabase<>nil then
sql="select max("+keyfield
sql=sql+") from "+table
rs=myDatabase.SQLSelect(sql)
if rs<>nil then
i=rs.idxField(1).integerValue
return i+1
rs.close
else
return 1
end if
end if
End Function
To get the next id for some given field, call it like this:
dim newID as integer
newID=getNextID("people","id")
Hope this helps!
--
brad at truetech dot org
http://truetech.org
<><
_______________________________________________
Unsubscribe or switch delivery mode:
<http://support.realsoftware.com/listmanager/>
Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>
|