realbasic-nug
[Top] [All Lists]

Re: How do you copy from one table to another?

To: REALbasic Network Users Group <realbasic-nug at lists dot realsoftware dot com>
Subject: Re: How do you copy from one table to another?
From: Terry Hulseberg <RB at Hulseberg dot com>
Date: Wed, 27 Feb 2002 18:40:14 -0700
You're just going to read in the records from table1 into some temporary
variables and write them back to table2. Here's an example:

  Dim DBF, REALdb As DatabaseCursor
  Dim r As DatabaseRecord
  //I don't think I would use REALdb as a cursor name
  
  DBF = db.SQLSelect("SELECT * FROM Table1") //or whatever
  
  While NOT DBF.EOF
    tempInteger1 = DBF.Field("ID").IntegerValue
    tempString2 = DBF.Field("Name").StringValue
    tempInteger2 = DBF.Field("Age").IntegerValue
    //...and so on with all fields
    //You could use an array to store the values or you could
    //do this one field at a time to save variables if both
    //databases and records already exist.
    DBF.MoveNext
    
    //If the record in the 2nd db does not exist then do this:
    r=New DatabaseRecord
    r.IntegerColumn("ID") = tempInteger1
    r.Column("Name") = tempString1
    r.IntegerColumn("Age") = tempInteger2
    //...and so on with all fields
    App.db.InsertRecord("Table2",r) //db2 if it's a different file
    
  WEND
  DBF.close
  db.commit
  db2.commit //only if you have two different dbs open.
  
//END of routine-------------------------------------

  //If the records exist (and are in the same order) then do this instead of
  //the New DatabaseRecord shown above...
  
  REALdb = db.SQLSelect("SELECT * FROM Table2")
  //or db2 if it's in a completely different file
  
  REALdb.edit
  REALdb.Field("ID").IntegerValue =
  REALdb.Field("Name").StringValue =
  REALdb.Field("Age").IntegerValue =
  //...and so on with all fields
  REALdb.update
  REALdb.close

If the records exist and you're not sure what order they're in then you
should do both SELECTS with a conditional like this:

DBF = db.SQLSelect("SELECT * FROM Table1 WHERE ID = " + str(IDValue))
DBF = db.SQLSelect("SELECT * FROM Table2 WHERE ID = " + str(IDValue))
Where IDValue is an integer containing whatever ID you're looking for.

Clear as mud?

--

> This should be easy, but I can't find anything in the documentation (online
> or paper) and I find the docs on databases to be a little sparse.
> 
> I've opened a DBF cursor and a REALdb cursor. The table structure  for both
> are identical -- same number of fields, same order, same data types.
> 
> I would like to copy a record (a row of fields) from the DBF table and
> insert it into the REALdb table. Is there any easy way to do this?
> 
> Thanks for any info you can provide.

--
Terry Hulseberg
HULSEBERG CONSULTING
http://www.hulseberg.com
+1.720.294.9665 eFAX
+1.303.523.0050 Worldwide Mobile (GSM)



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