gettingstarted
[Top] [All Lists]

Re: SQL Error with REAL Database

To: Getting Started <gettingstarted at lists dot realsoftware dot com>
Subject: Re: SQL Error with REAL Database
From: Kristin Green <kristin at greenapple dot on dot ca>
Date: Wed, 30 Jun 2004 12:47:53 -0400
Delivered-to: gettingstarted at lists dot realsoftware dot com
List-help: <mailto:gettingstarted-request@lists.realsoftware.com?subject=help>
List-id: Getting Started <gettingstarted.lists.realsoftware.com>
List-post: <mailto:gettingstarted@lists.realsoftware.com>
References: <5283CF7E-CAAE-11D8-B11D-000A9586AE9A at greenapple dot on dot ca> <1E3FAFD1-CAAF-11D8-929D-000D9336142C at truetech dot org> <09194427-CAB1-11D8-B11D-000A9586AE9A at greenapple dot on dot ca> <B40E38FA-CAB2-11D8-929D-000D9336142C at truetech dot org>
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>

<Prev in Thread] Current Thread [Next in Thread>