FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour DBF to Mysql
Posts: 74
Joined: Sun Aug 27, 2017 07:18 PM
DBF to Mysql
Posted: Fri May 13, 2022 12:18 PM
¡Buen día!
Tengo tablas dbf de más de una empresa y ahora quiero unirlas todas en una sola tabla en mysql además de crear la tabla donde esta tabla aún no existe en mysql. Otro detalle que no serán todos los campos existentes en la tabla dbf por eso aprovecho para eliminar campos innecesarios. Estoy intentando con el siguiente código:

Code (fw): Select all Collapse
#include "fivewin.ch"
#include "dbcombo.ch"
#include "dtpicker.ch"

function main()

local cBaseDir := "C:\fwh\samples\" , cTabela := "customer" , aStruct:={}


oCn   := FW_DemoDB(6)

if !oCn:TableExists( "test000" )
   
   aStruct:={} 
   aadd(  aStruct, {"FIRST"   , "C", 20,  0})
   aadd(  aStruct, {"CITY"    , "C", 30,  0})
   aadd(  aStruct, {"STATE"   , "C",  2,  0})
   aadd(  aStruct, {"ZIP"     , "D", 10,  0})
   aadd(  aStruct, {"SALARY"  , "N",  9,  2})
   oCn:CreateTable( "test000" ,aStruct , .T. )

endif 

if oCn:TableExists( "test000" )
      MsgWait("Aguarde importando a tabela : "+cTabela, "Aguarde",2 )

      oRec := oCn:RowSet( "select * from test000 "   )
      oDbf :=TDataBase():Open( nil, cBaseDir+cTabela, "dbfcdx", .F. )  

      oCn:SetAutoCommit( .f. )
      While !oDbf:Eof()
          oRec:Append()
          oRec:FIRST   := oDbf:FIRST    
          oRec:CITY    := oDbf:CITY     
          oRec:STATE   := oDbf:STATE    
          oRec:ZIP     := oDbf:ZIP      
          oRec:SALARY  := oDbf:SALARY   
          oRec:Save()
          oDbf:Dbskip()
      enddo
      oCn:SetAutoCommit( .t. )
   

endif 


xbrowser oCn:test000


oCn:DropTable( "test000" )

XBROWSER oCn:ListTables TITLE "SELECT TO VIEW"

return .t.
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: DBF to Mysql
Posted: Fri May 13, 2022 05:40 PM
If you are appending records from many DBF files into a single table.
You can also try:
Code (fw): Select all Collapse
USE CUSTOMER

if oCn:TableExists( "test000" )
   oCn:Insert( "test000", "FIRST,CITY,STATE,ZIP,SALARY", ;
           FW_DbfToArray( "FIRST,CITY,STATE,ZIP,SALARY" ) )
endif
Regards



G. N. Rao.

Hyderabad, India
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: DBF to Mysql
Posted: Fri May 13, 2022 05:49 PM
If you are importing one single DBF into one table, you can also try:
Code (fw): Select all Collapse
oCn:ImportFromDBF( "customer.dbf", "test000", nil, nil, { ;
   {"FIRST"   , "C", 20,  0} ,'
   {"CITY"    , "C", 30,  0} ,;
   {"STATE"   , "C",  2,  0} ,;
   {"ZIP"     , "D", 10,  0} ,;
   {"SALARY"  , "N",  9,  2}  } )
Regards



G. N. Rao.

Hyderabad, India
Posts: 74
Joined: Sun Aug 27, 2017 07:18 PM
Re: DBF to Mysql
Posted: Mon Jul 18, 2022 06:20 PM

Mui grato!

Continue the discussion