FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour DBF to SQL converter program
Posts: 117
Joined: Thu Jan 08, 2015 09:27 AM

DBF to SQL converter program

Posted: Tue Jul 28, 2015 09:41 AM
Hello,

I am making a program which reads all the dbf files in a folder, and then each dbf file will have corresponding sql table in a MYSQL database.

1: Choose a SQL database name
2: Choose a folder, to find all dbf's in it.
3: Make a MYSQL database with the name given by step 1.
4: Each dbf file becomes a table in the MYSQL database. each sql table has the name of the dbf file. (So a file customer.dbf becomes customer in MySQL)
5: import the data from each dbf file to the sql tables.

The code look like this:

Code (fw): Select all Collapse
FUNCTION CreateDatabaseIfNotYetExist( vardb )
   local oError
   
   ADOCONNECT oCn TO MYSQL SERVER localhost USER root PASSWORD <mysqlpassword>
 
   if oCn == nil
      MsgInfo( "Not connected" )
    else
      if oCn:State > 0
         MsgInfo( "open" )
         
      TRY
         oCn:Execute( "CREATE DATABASE " + vardb ) //This works, a database name which I chose, is created.
         MsgInfo( "created" )      
      CATCH oError
         MsgInfo( "The database already exists" )
      END          
         
      else
         MsgInfo( "not open" )
      endif
   endif         
 
   oCn:Close() 
  
Return nil


Code (fw): Select all Collapse
FUNCTION ConnectWithDatabase( vardb )
   //ADOCONNECT oCn TO MYSQL SERVER localhost DATABASE vardb USER root PASSWORD <mysqlpassword> // PASSWORD ...   (I tried to put the variable vardb in the Command, but as I already thought this did not work) 
   oCn := FW_OpenAdoConnection( "MYSQL", "localhost",vardb, "root", "mysqlpassword" )   //I tried also this.
   
   if oCn == nil .or. oCn:State < 1
      MsgInfo( "Connect failed" )
      return nil
   endif         
 
   MsgInfo( "Connection Open" )
RETURN NIL



Code (fw): Select all Collapse
local oRs, oWnd, sqldatabasename := Space( 20 )
   DEFINE WINDOW oWnd TITLE "DBFTOSQLTOOL"
    
   @1.8, 3 SAY "sqldatabasename: " OF oWnd
   @2,15 GET sqldatabasename OF oWnd            
   
   ACTIVATE WINDOW oWnd      
            
   CreateDatabaseIfNotYetExist( sqldatabasename )
   ConnectWithDatabase( sqldatabasename )
   
   FW_AdoImportFromDBF( oCn, "C:\Pieter\Dev\import\customer.dbf")
   
   oRs = TRecSet():New():Open( "customer", oCn )
   
   if oRs:IsOpen()
 
      XBROWSER oRs // SETUP oBrw:lIncrFilter := .T. 
               // SETUP oBrw:bEdit := { | oRec | MyEdit( oRec ) }
                 // SETUP oBrw:lIncrSearch := .T.
                   // SETUP oBrw:lWildSeek := .T.                   
 
      oRs:Close()
   else
      MsgAlert( "The recordset could not be opened" )
      MsgInfo( "Check that you have REQUEST DBFCDX" )      
   endif


Code (fw): Select all Collapse
How can I use the command ADOCONNECT or FW_OpenAdoConnection to connect with the database which I just created.
ADOCONNECT oCn TO MYSQL SERVER localhost DATABASE vardb USER root PASSWORD <mysqlpassword> // PASSWORD ...   (I tried to put the variable vardb in the Command, but as I already thought this did not work) 
oCn := FW_OpenAdoConnection( "MYSQL", "localhost",vardb, "root", "mysqlpassword" )  //I tried also this, but it did not work.


Anybody an idea.

Best regards,

Pieter
Posts: 1600
Joined: Fri Oct 07, 2005 05:56 PM

Re: DBF to SQL converter program

Posted: Tue Jul 28, 2015 11:09 AM

Dear Pieter,

You can find DBF2SQL in this forum, it automatic create MySql data from DBF File.

Regards,

Dutch



FWH 2304 / xHarbour Simplex 1.2.3 / BCC73 / Pelles C / UEStudio

FWPPC 10.02 / Harbour for PPC (FTDN)

ADS V.9 / MySql / MariaDB

R&R 12 Infinity / Crystal Report XI R2

(Thailand)
Posts: 117
Joined: Thu Jan 08, 2015 09:27 AM

Re: DBF to SQL converter program

Posted: Tue Jul 28, 2015 11:55 AM

Hello Dutch:),

thanks for your reply.

I already could convert dbf to sql, but I want to automate it for a lot of dbf files and the option to choose you own sql database name.

If there exist a sql database name "DB1", I could write the following:

ADOCONNECT oCn TO MYSQL SERVER localhost DATABASE DB1 USER root PASSWORD mysqlpassword //this works

but what if I have a variable vardb := "DB2" (Which I earlier have created with oCn:Execute( "CREATE DATABASE " + vardb ) )

and I want to connect:

ADOCONNECT oCn TO MYSQL SERVER localhost DATABASE vardb USER root PASSWORD mysqlpassword //vardb should literally replaced by DB2 and then it should work also.

Somehow I can not get this work.

Maybe you know it, or where can I find a solution in a post.

Posts: 1600
Joined: Fri Oct 07, 2005 05:56 PM

Re: DBF to SQL converter program

Posted: Tue Jul 28, 2015 12:15 PM

could you try
&vardb
or
(vardb)

Regards,

Dutch



FWH 2304 / xHarbour Simplex 1.2.3 / BCC73 / Pelles C / UEStudio

FWPPC 10.02 / Harbour for PPC (FTDN)

ADS V.9 / MySql / MariaDB

R&R 12 Infinity / Crystal Report XI R2

(Thailand)
Posts: 117
Joined: Thu Jan 08, 2015 09:27 AM

Re: DBF to SQL converter program

Posted: Tue Jul 28, 2015 12:28 PM

Yes:D, thank you very much.

&vardb works and (vardb) also.

Pieter

Posts: 117
Joined: Thu Jan 08, 2015 09:27 AM

Re: DBF to SQL converter program

Posted: Tue Jul 28, 2015 01:16 PM
Hello,

Question:
How can I pick a directory or file from the Microsoft Windows Explorer easily? I think I have seen it in the fivewin sample directory in a .prg file once
Code (fw): Select all Collapse
  local oRs, oWnd, sqldatabasename := Space( 20 ), dbfPath := Space( 20 )
  DEFINE WINDOW oWnd TITLE "DBFTOSQLTOOL"
    
   @1.8, 3 SAY "sqldatabasename: " OF oWnd
   @2,15 GET sqldatabasename OF oWnd        
   @3.8, 3 SAY "Choose directorypath: " OF oWnd   //here I want acces to the file explorer, (so I can choose for example C:\Pieter\DATA\ or C:\Pieter\DATA\customer.dbf, maybe there is a .prg file which can do this easily.  
   @4, 15 GET dbfPath OF oWnd  //

   ACTIVATE WINDOW oWnd
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM

Re: DBF to SQL converter program

Posted: Tue Jul 28, 2015 02:04 PM

Peiter,

You can use cGETDIR() to get a directory name.

And try cGETFILE() to get the filename. I'm not sure if it includes the path.

James

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 117
Joined: Thu Jan 08, 2015 09:27 AM

Re: DBF to SQL converter program

Posted: Tue Jul 28, 2015 03:08 PM
James,

Thank you!, I got now a lot of things working:D. I still got error-messages, however it seems that the data is converted well from dbf to sql

Part of the code:
Code (fw): Select all Collapse
dbfPath := aGETFILES("DataBase | *.dbf")

FOR x:=1 to len(dbfPath)    
       FW_AdoImportFromDBF( oCn, dbfPath[x])
Next


Pieter
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM

Re: DBF to SQL converter program

Posted: Tue Jul 28, 2015 03:34 PM

I am not familiar with aGetFiles(). How do you specify a path?

James

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 1600
Joined: Fri Oct 07, 2005 05:56 PM

Re: DBF to SQL converter program

Posted: Wed Jul 29, 2015 02:29 AM
Pieter,

You should select folder and get dbfPath.
Code (fw): Select all Collapse
cFolder := cGetDir('Select Folder')
dbfPath = directory(cFolder +"\*.dbf")
FOR x:=1 to len(dbfPath)    
       FW_AdoImportFromDBF( oCn, dbfPath[x][1])
Next

pieter wrote:James,

Thank you!, I got now a lot of things working:D. I still got error-messages, however it seems that the data is converted well from dbf to sql

Part of the code:
Code (fw): Select all Collapse
dbfPath := aGETFILES("DataBase | *.dbf")

FOR x:=1 to len(dbfPath)    
       FW_AdoImportFromDBF( oCn, dbfPath[x])
Next


Pieter
Regards,

Dutch



FWH 2304 / xHarbour Simplex 1.2.3 / BCC73 / Pelles C / UEStudio

FWPPC 10.02 / Harbour for PPC (FTDN)

ADS V.9 / MySql / MariaDB

R&R 12 Infinity / Crystal Report XI R2

(Thailand)
Posts: 117
Joined: Thu Jan 08, 2015 09:27 AM

Re: DBF to SQL converter program

Posted: Wed Jul 29, 2015 08:09 AM
James Bott wrote:I am not familiar with aGetFiles(). How do you specify a path?

James,

When I searched for cGetDir() and cGetFile() (http://wiki.fivetechsoft.com/doku.php?i ... n_cgetfile) in a searchengine, I found also aGetFiles() http://wiki.fivetechsoft.com/doku.php?i ... _agetfiles. With aGetFiles() one can select multiple files in a chosen directory.

Pieter

James
Posts: 117
Joined: Thu Jan 08, 2015 09:27 AM

Re: DBF to SQL converter program

Posted: Wed Jul 29, 2015 08:44 AM
dutch wrote:Pieter,

You should select folder and get dbfPath.
Code (fw): Select all Collapse
cFolder := cGetDir('Select Folder')
dbfPath = directory(cFolder +"\*.dbf")
FOR x:=1 to len(dbfPath)    
       FW_AdoImportFromDBF( oCn, dbfPath[x][1])
Next

pieter wrote:James,

Thank you!, I got now a lot of things working:D. I still got error-messages, however it seems that the data is converted well from dbf to sql

Part of the code:
Code (fw): Select all Collapse
dbfPath := aGETFILES("DataBase | *.dbf")

FOR x:=1 to len(dbfPath)    
       FW_AdoImportFromDBF( oCn, dbfPath[x])
Next


Pieter


Dutch,

Thanks for this solution, this solution you gave me is actually what I described in the beginning of the topic. (The solution that I made with aGetFiles() also works, here I have to select the files in a chosen directory)

Code (fw): Select all Collapse
cFolder := cGetDir('Select Folder')
dbfPath := directory(cFolder +"\*.dbf")
FOR x:=1 to len(dbfPath)    
       FW_AdoImportFromDBF( oCn, cFolder + "\" + dbfPath[x][1]) //I have added cFolder + "\" to get your code working. 
Next


Pieter
Posts: 117
Joined: Thu Jan 08, 2015 09:27 AM

Re: DBF to SQL converter program

Posted: Wed Jul 29, 2015 09:04 AM

I am searching now for a function which puts all subdirectory names of a chosen directory in a array.

Example:
directory: C:\Pieter\DBF2SQLtool\data

01
02
file1.dbf
file2.dbf

In directory 01 and 02 are also dbf files. which I can convert to sql with the code I already have.

Maybe somebody knows if this function to get all subdirectory names in a directory exist, or another solution?

Kind regards,

Pieter

Posts: 44229
Joined: Thu Oct 06, 2005 05:47 PM

Re: DBF to SQL converter program

Posted: Wed Jul 29, 2015 09:11 AM
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 117
Joined: Thu Jan 08, 2015 09:27 AM

Re: DBF to SQL converter program

Posted: Wed Jul 29, 2015 09:53 AM
Antonio,

thank you.

Code (fw): Select all Collapse
cFolder := cGetDir('Select Folder')
dbfPath := directory(cFolder +"\*.dbf")
//subdirectories := getSubdirectoryNames(C:\Pieter\DBFTOSQLTOOL\DATA) //I have invented this function myself.
MsgInfo(curDir())
MsgInfo(ADir("??")) //I tried this, to count all the subdirectories, 01 and 02, but it gives 0 and I thought it should be 2.

FOR x:=1 to len(dbfPath)    
       FW_AdoImportFromDBF( oCn, cFolder + "\" + dbfPath[x][1])
Next


I think I have to do something with cFileMask http://wiki.fivetechsoft.com/doku.php?i ... _cfilemask, but I don't know how to get directory names.

Pieter