José M. C. Quintas
unread,Sep 15, 2023, 12:35:42 PM9/15/23Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to minigu...@googlegroups.com
Create you own routine.
Source code as an example using ADO
Make your own tests about codepage and others.
It is part of my application, then, if compile will show that need more
functions, but using name you can create them.
Or you can create a file and import using heidisql or another tool.
#define SQL_MAX_CMDINSERT 256000
FUNCTION CopyDbfToSQL( cTable, lTransfere, lCria, lZera, cNewTable )
LOCAL oStru
LOCAL cSQL, xValue, nCont, cSQLFix
LOCAL lBegin := .T., cTxt, cKeyName
LOCAL cnGERAL := ADOLocal()
LOCAL nSelect := Select()
IF ! File( cTable + iif( ".DBF" $ Upper( cTable ), "", ".DBF" ) )
RETURN Nil
ENDIF
hb_Default( @lCria, .F. )
hb_Default( @lZera, .F. )
hb_Default( @cNewTable, cTable )
cTable := Upper( cTable )
SELECT 0
USE ( cTable ) ALIAS thisdbf
oStru := dbStruct()
cKeyName := "ID" + iif( Left( cTable, 2 ) $ "JP,HL", Substr( cTable,
3 ), cTable )
USE
cSQL := "CREATE TABLE IF NOT EXISTS " + cNewTable + " ( " + cKeyName
+ " INT(9) NOT NULL AUTO_INCREMENT, "
FOR nCont = 1 TO Len( oStru )
IF oStru[ nCont, DBS_NAME ] != cKeyName
cSQL += oStru[ nCont, DBS_NAME ] + " "
DO CASE
CASE oStru[ nCont, DBS_TYPE ] == "N"
IF oStru[ nCont, DBS_DEC ] == 0
cSQL += " INT( " + Ltrim( Str( oStru[ nCont, DBS_LEN ] )
) + " ) DEFAULT 0"
ELSE
cSQL += " DECIMAL( " + Ltrim( Str( oStru[ nCont, DBS_LEN
] ) ) + " , " + Ltrim( Str( oStru[ nCont, DBS_DEC ] ) ) + " ) DEFAULT 0"
ENDIF
CASE oStru[ nCont, DBS_TYPE ] == "C"
IF oStru[ nCont, DBS_LEN ] < 250
cSQL += " VARCHAR( " + Ltrim( Str( oStru[ nCont, DBS_LEN
] ) ) + " ) DEFAULT '' "
ELSE
cSQL += " TEXT"
ENDIF
CASE oStru[ nCont, DBS_TYPE ] == "D"
cSQL += " DATE " // DEFAULT '0000-00-00'"
CASE oStru[ nCont, DBS_TYPE ] == "M"
cSQL += " TEXT "
ENDCASE
cSQL += " , "
ENDIF
NEXT
cSQL += " PRIMARY KEY ( " + cKeyName + " )"
cSQL += " )"
cSQL += ";"
SayScroll( "Salvando no SQL." + cTable )
IF lCria
cnGERAL:ExecuteNoReturn( cSQL )
ENDIF
IF lZera
cnGERAL:ExecuteNoReturn( "TRUNCATE TABLE " + cNewTable )
ENDIF
IF ! lTransfere
SELECT ( nSelect )
RETURN Nil
ENDIF
SELECT 0
USE ( cTable ) ALIAS thisdbf
GrafTempo( "Processando " + cTable )
cSQLFix := "INSERT INTO " + cNewTable + " ( "
FOR nCont = 1 TO FCount()
cSQLFix += FieldName( nCont )
IF nCont != FCount()
cSQLFix += ", "
ENDIF
NEXT
cSQLFix += " ) VALUES "
cTxt := ""
DO WHILE ! Eof()
GrafTempo( RecNo(), LastRec() )
Inkey()
cSQL := "( "
FOR nCont = 1 TO FCount()
xValue := FieldGet( nCont )
DO CASE
CASE ValType( xValue ) == "N"
cSQL += NumberSQL( xValue )
CASE ValType( xValue ) == "D"
cSQL += DateSQL( xValue )
CASE ValType( xValue ) == "C"
xValue := SQLValidString( xValue )
cSQL += StringSQL( xValue )
OTHERWISE
cSQL += "NULL"
ENDCASE
IF nCont != FCount()
cSQL += ","
ENDIF
NEXT
cSQL += " )"
IF Len( cTxt ) == 0
cTxt += cSQLFix
lBegin := .T.
ENDIF
IF ! lBegin
cTxt += ", "
ENDIF
lBegin := .F.
cTxt += cSQL
IF Len( cTxt ) > SQL_MAX_CMDINSERT
cnGERAL:ExecuteNoReturn( cTxt )
cTxt := ""
ENDIF
SKIP
ENDDO
IF Len( cTxt ) != 0
cnGERAL:ExecuteNoReturn( cTxt )
ENDIF
USE
SELECT ( nSelect )
RETURN Nil
Note when creating you own routine:
- define codepage on connection
- define codepage on database (mysql default is not the same on all
versions)
- insert many records as possible, one by one is slow
- check data after write, about codepage
- command limit from station depends on mysql setup, may be 4MB or more,
on this routine I fix to 256KB.
José M. C. Quintas