Copying Tables between databases

Hi all,

The latest BaseGuide 6.2 pdf Chapter 9 page 389 has a description and sample code of how to import a table from database A to database B and how to append records from the source.

I am not clear from the documentation if this code is designed for embedded or external dbs or both.

I have created a quick mockup using HSQL2.5 backends, however the code copied from the original documentation throws up an error message. Tried to rem out the duplicate declaration to no success.

In the linked mockup DummyB is supposed to import from DummyA.
Has anyone come across this feature? If that could be made workable, that would be fantastic because it would open up the door to synchronise tables between dbs( a la replication light) assuming no one touches the source db. Anyone able to spot the problem with the code ?
Thanks for your thoughts and ideas. Please keep save.

Hello,

Did not bother with your attachments. Please just zip & post here changing the ext to .odb and note it must be changed to .zip

Typically don’t deal with rar files.

The code has a number of naming errors. Even after correcting this nothing appears to happen. From the appearance of the code it is simply using SQL to read records in one table and write to another. May be a bit of code there for updating but to me not worth the effort.

This is not a new concept. Even if this worked (and the process seems backward) there is a matter of keeping things synced.

Edit:

Got the code to work. A number of errors in the documentation. Tested with HSQLDB embedded to HSQLDB embedded.

Here is the corrected code:

Sub DataCopy
Dim oDatabaseContext As Object
Dim oDatasource As Object
Dim oDatasourceZiel As Object
Dim oConnection As Object
Dim oConnectionZiel As Object
Dim oDB As Object
Dim oSQL_Command As Object
Dim oSQL_CommandTarget As Object
Dim oResult As Object
Dim oResultTarget As Object
Dim stSql As String
Dim stSqlTarget As String
Dim inID As Integer
Dim inIDTarget As Integer
Dim stName As String
Dim stTown As String
oDB = ThisComponent.Parent
stDir = Left(oDB.Location,Len(oDB.Location)-Len(oDB.Title))
          Rem Modify to your odb
stDir = ConvertToUrl(stDir & "TargetDB.odb")
oDatasource = ThisComponent.Parent.CurrentController
If Not (oDatasource.isConnected()) Then
oDatasource.connect()
End If
oConnection = oDatasource.ActiveConnection()
oDatabaseContext = createUnoService("com.sun.star.sdb.DatabaseContext")
oDatasourceTarget = oDatabaseContext.getByName(stDir)
oConnectionTarget = oDatasourceTarget.GetConnection("","")
oSQL_Command = oConnection.createStatement()
        Rem Modify to your table
stSql = "SELECT * FROM ""CUSTOMER"""
oResult = oSQL_Command.executeQuery(stSql)
While oResult.next
inID = oResult.getInt(1)
stName = oResult.getString(2)
stTown = oResult.getString(3)
oSQL_CommandTarget = oConnectionTarget.createStatement()
        Rem Modify to your table
stSqlTarget = "SELECT ""ID"" FROM ""CUSTOMER"" WHERE ""ID"" = '"+inID+"'"
'oResultTarget = oSQL_CommandZiel.executeQuery(stSqlTarget) '
oResultTarget = oSQL_CommandTarget.executeQuery(stSqlTarget)
inIDTarget = - 1
While oResultTarget.next
inIDTarget = oResultTarget.getInt(1)
Wend
If inIDTarget = - 1 Then
        Rem Modify to your table
stSqlZiel = "INSERT INTO ""CUSTOMER"" (""ID"",""NAME"",""ADDRESS"") VALUES ('"+inID+"','"+stName+"','"+stTown+"')"
oSQL_CommandTarget.executeUpdate(stSqlZiel)
End If
Wend
End Sub

Oh well, thanks anyway

Was just looking at the code again and it is missing some logic. Supposedly meant to copy records in one table missing in another table. Has no concern for existing records being up-to-date.

Seems to be better to simply empty the receiving table of records and then just copy all. Of course this could be a problem in very large tables.

@gkick,

This is not new in the documentation. It is also in Chapter 9 of the v5.x documentation. It is wrong there too. Surprisingly, the 6.2 documentation was noted as being translated from German. I have not found this routine in that version. Also there are items in the German version not in the translated version.

Yep, although stuff can get lost in translation and does, perhaps a little QC issue. Anyway, thanks for having a second look. Guess it could be adapted to work with back ends as well, only problem I can see is separate folder structure and a fight between 2 hsql jars.