Page 1 of 1

DBF to Mysql

Posted: Fri May 13, 2022 12:18 pm
by jair
¡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: Select all | Expand


#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.



Re: DBF to Mysql

Posted: Fri May 13, 2022 5:40 pm
by nageswaragunupudi
If you are appending records from many DBF files into a single table.
You can also try:

Code: Select all | Expand

USE CUSTOMER

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

Re: DBF to Mysql

Posted: Fri May 13, 2022 5:49 pm
by nageswaragunupudi
If you are importing one single DBF into one table, you can also try:

Code: Select all | Expand

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}  } )
 

Re: DBF to Mysql

Posted: Mon Jul 18, 2022 6:20 pm
by jair
Mui grato!