Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3081
  • Last Modified:

how can I compare two databases in foxpro to check if they are identical?

I have two databases in foxpro and want to check if the records inside are all identical including data type.
are there any means I can do so?
0
hkgal
Asked:
hkgal
  • 2
  • 2
  • 2
  • +1
1 Solution
 
jrbbldrCommented:
There would be 3 possible things necessary to check for IDENTICAL
  1. Field Names
  2. Field Parameters - Type, Size
  3. Field Contents

The first 2 are easy using the AFIELDS() function.
You can create an array on each of the 2 tables describing the field info and then run a comparison of the 2 field info array values.

The 3rd item is more involved since you would need to scan each record and then, within that record, compare each field value against the other table's matching record's field value.

The last thing to check, not listed above, would be Indicies on each table.
You can do that using the TAG() and KEY() functions

Good Luck

0
 
hkgalAuthor Commented:
sorry i am new to foxpro, don't really know how to work it out...

for e.g., the table structure as:
ID        Name       Sales
01       KL             `100
23       Peter           200
0
 
Olaf DoschkeSoftware DeveloperCommented:
It's not really that simple, but if you're asking about structure AND data in dbfs, then the files must be identical, eg a simplistic check would be:

FILETOSTR('c:\data\db1\table.dbf')==FILETOSTR('c:\data\db2\table.dbf')

If the order of data can be different and there can be a difference in deleted records existing in one dbf and not the other, then it's harder to determine.

Bye, Olaf.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Olaf DoschkeSoftware DeveloperCommented:
You can compare structure using AFIELDS, but if dbfs are part of a dbc each you can also simply compare data within each dbc file, as a dbc file (together witch dcx and dct file) is nothing more or less than a free dbf with data about it's tables and more.

Bye, Olaf.
0
 
CaptainCyrilFounder, Software Engineer, Data ScientistCommented:
The easiest way to check everything at once is to do the following:

1) Copy both tables to SDF format. It will depend on field width and contents
USE table1
COPY TO file1.sdf TYPE SDF
USE table2
COPY TO file2.sdf TYPE SDF

2) Use DOS comment FC to compare for both files
FC file1.sdf file2.sdf
0
 
jrbbldrCommented:
Olaf's and CaptainCyril's advice above is good.

However if you want to learn some more Foxpro, you might want to study the following (I hope there aren't too many typos in it...)

Dimension aryTbl1Flds (1)
USE Table1 IN 0
SELECT Table1
nFldCnt1 = AFIELDS(aryTbl1Flds)  && Put list of field info into array - use AFIELDS()
nTbl1RecCount = RECCOUNT()  && Get record count

Dimension aryTbl2Flds (1)
 USE Table2 IN 0
SELECT Table2
nFldCnt2 = =AFIELDS(aryTbl2Flds)
nTbl2RecCount = RECCOUNT()
 
lTablesSame = .T.  && Start Off Assuming Tables Are SAME

* --- Check If Field Count Is Same ---
IF nFldCnt1 = nFldCnt2
   * --- Number of Fields Same, Now Check Each Field's Name,Type,Size, etc ---
   FOR Cntr = 1 TO nFldCnt1
       IF aryTbl1Flds(Cntr) # aryTbl2Flds(Cntr)
            * --- Field Type,Size, etc. Different - TABLES NOT SAME ---
            lTablesSame = .F.
            EXIT
       ENDIF
   ENDFOR
ELSE
  * --- Field Count D ifferent - TABLES NOT SAME ---
   lTablesSame = .F.
ENDIF

IF lTablesSame
   * --- Table Field Parameters SAME, Now check field contents ---
   IF nTbl1RecCount = nTbl2RecCount
        * --- Same Number of Records In Both Tables ---
        SELECT Table1
        SCAN
            nRecno = RECNO
           
            * --- Check Each Field Value For This Record In Both Tables ---
            FOR FldCntr = 1 TO nFldCnt1
                  cFldName = aryTbl1Flds(FldCntr,1)  && Get Field Name
                  Value1 = EVAL(cFldName)  & Get Field Value
                 
                  SELECT Table2
                  GO nRecno
                  Value2 = EVAL(cFldName)
                   
                  IF Value1 # Value2
                      * --- Field Value In Table1 NOT EQUAL TO Field Value in Table 2 ---
                        lTablesSame = .F.
                       EXIT
                  ENDIF      
            ENDFOR

           SELECT Table1
       ENDSCAN
   ELSE
       * --- Record Count Different - TABLES NOT SAME ---
       lTablesSame = .F.
   ENDIF
ENDIF

* --- I'll skip checking Table Indicies at this time ---

IF NOT lTablesSame
   WAIT WINDOW "Tables NOT SAME! " 
ENDIF

Good Luck

0
 
hkgalAuthor Commented:
excellent! Thanks!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now