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: Select all
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: Select all
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: Select all
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: Select all
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.
Best regards,
Pieter