xharbour com mysql
Enviado: 20 Mar 2005 23:49
Ola
lembro que vi uma mensagem de exemplo de xharbour+mysql
entao ai vai
#include "sqlrdd.ch"
#define RECORDS_IN_TEST 1000
#define SQL_DBMS_NAME 17
#define SQL_DBMS_VER 18
/*------------------------------------------------------------------------*/
Function Main( cDSN, lLog )
/* CODE_IS is the primary key 1st key. See SQLRDD.CH for details about structure array */
local aStruct := {{"CODE_ID","C",8,0,.F.,,,,,1 },{"CARDID","C",1,0},{"DESCR","C",50,0},{"PERCENT","N",10,2},{"DAYS","N",8,0},{"DATE_LIM","D",8,0},{"ENABLE","L",1,0},{"OBS","M",10,0}, {"VALUE","N",18,6}}
local nCnn, i, s
? ""
? "demo.exe"
? ""
? "Small SQLRDD demo"
? "(c) 2003 - Marcelo Lombardo"
? ""
Connect( cDSN ) // see connect.prg
? "Connected to :", SR_GetConnectionInfo(, SQL_DBMS_NAME ), SR_GetConnectionInfo(, SQL_DBMS_VER )
If lLog != NIL
? "Starting LOG", SR_GetActiveConnection(), SR_StartLog()
endif
// SR_SetSyntheticIndex(.t.)
? "Creating table :", dbCreate( "TEST_TABLE", aStruct, "SQLRDD" )
USE "TEST_TABLE" SHARED VIA "SQLRDD"
? "Table opened. Alias :", select(), alias(), RddName()
? "Fieldpos( CODE_ID ) :", Fieldpos( "CODE_ID" )
? "Fieldpos( DESCR ) :", Fieldpos( "DESCR" )
? "Creating 02 indexes..."
s := seconds()
Index on CODE_ID+DESCR to TEST_TABLE_IND01
Index on str(DAYS)+dtos(DATE_LIM) to TEST_TABLE_IND02
? "Done, Elapsed time :", seconds() - s, "seconds"
? ""
? "Appending " + alltrim(str(RECORDS_IN_TEST)) + " records.."
s := seconds()
For i = 1 to RECORDS_IN_TEST
Append Blank
Replace CODE_ID with strZero( i, 5 )
Replace DESCR with dtoc( date() ) + " - " + time()
Replace DAYS with (RECORDS_IN_TEST - i)
Replace DATE_LIM with date()
Replace ENABLE with .T.
Replace OBS with "This is a memo field. Seconds since midnight : " + alltrim(str(seconds()))
Next
? "Done, Elapsed time :", seconds() - s, "seconds"
? ""
? "dbClearIndex() :", dbClearIndex()
? "dbCloseArea() :", dbCloseArea()
? ""
? "Press any key to open the table"
inkey(0)
USE "TEST_TABLE" SHARED VIA "SQLRDD"
? "Opening Indexes"
SET INDEX TO TEST_TABLE_IND01
SET INDEX TO TEST_TABLE_IND02 ADDITIVE
? "Set Order to 1 :", OrdSetFocus(1)
? "Seek :", dbSeek( "00002" )
? "found() :", found()
? "Recno(),bof(),eof() :", recno(), bof(), eof()
? "dbUnLock() :", dbUnLock()
? "RLock(), dbRLockList:", rlock(), sr_showVector( dbRLockList() )
? "Writes to the WA :", FIELD->DESCR := "Hello, SQL!", FIELD->PERCENT := 23.55
? "dbCommit() :", dbCommit()
? " "
? "Press any key to browse()"
inkey(0)
clear
browse(row()+1,1,row()+20,80)
clear
? "Order 2, key is :", OrdSetFocus(2), ordKey()
OrdScope( 0, str(RECORDS_IN_TEST / 4,8) )
OrdScope( 1, str(RECORDS_IN_TEST / 2,8) )
? "TOP Scope :", OrdScope( 0 )
? "BOTTOM Scope :", OrdScope( 1 )
? "Press any key to browse() with another index and scope"
inkey(0)
dbGoTop()
clear
browse(row()+1,1,row()+20,80)
Return NIL
#include "pgs.ch" // Needed if you plan to use native connection to Postgres
#include "mysql.ch" // Needed if you plan to use native connection to MySQL
#include "oracle.ch" // Needed if you plan to use native connection to Oracle
#include "firebird.ch" // Needed if you plan to use native connection to Firebird
REQUEST SQLRDD // SQLRDD should be linked in
REQUEST SR_ODBC // Needed if you plan to connect with ODBC
REQUEST SR_PGS // Needed if you plan to use native connection to Postgres
REQUEST SR_MYSQL // Needed if you plan to use native connection to MySQL
REQUEST SR_ORACLE // Needed if you plan to use native connection to Oracle
REQUEST SR_FIREBIRD // Needed if you plan to use native connection to Firebird
REQUEST DBFNTX
REQUEST DBFCDX
REQUEST DBFFPT
REQUEST DBFDBT
/*------------------------------------------------------------------------*/
Function Connect( cDatabase )
local nCnn, nDrv, cDriver, nOpt, nDetected, hIniFile, aKeys, nKey, cConnString
local oldScreen, hDsn
hIniFile := HB_ReadIni( "sqlrdd.ini", .F.,,.F. ) // Read ini file in a hash table
If hIniFile == NIL
? "Could not read from sqlrdd.ini"
Quit
EndIf
If cDatabase == NIL
aKeys := HGetKeys( hIniFile )
If len(aKeys) == 0
? "No connections available in sqlrdd.ini"
Quit
ElseIf len(aKeys) == 1
nKey := 1
Else
clear screen
@5,1 say PadC( "Choose connection option", 80 )
nKey := achoice( 7, 20, 20, 60, aKeys )
clear screen
If nKey == 0
? "No connection selected"
Quit
EndIf
EndIf
hDsn := HGetValueAt( hIniFile, nKey )
If !"CONNSTRING" IN hDsn
? "ConnString not found in " + aKeys[nKey]
Quit
EndIf
Else
If ! cDatabase IN hIniFile
? "Connection [" + cDatabase + "] not found in sqlrdd.ini"
Quit
EndIf
hDsn := hIniFile[ cDatabase ]
If !"CONNSTRING" IN hDsn
? "ConnString not found in " + cDatabase
Quit
EndIf
EndIf
cConnString := hDsn[ "CONNSTRING" ]
nDetected := DetectDBFromDSN( cConnString )
If nDetected > SYSTEMID_UNKNOW
? "Connecting to", cConnString
nCnn := SR_AddConnection( nDetected, cConnString )
Else
clear screen
nOpt := Alert( "Please, select connection type", { "ODBC", "Postgres", "MySQL", "Oracle", "Firebird" } )
If nOpt > 0
nCnn := SR_AddConnection( If( nOpt = 1, CONNECT_ODBC, if( nOpt = 2, CONNECT_POSTGRES, if( nOpt = 3, CONNECT_MYSQL, if( nOpt = 4, CONNECT_ORACLE, CONNECT_FIREBIRD ) ) ) ), cConnString )
Else
? "No connection type selected"
Quit
EndIf
EndIf
/* returns the connection handle or -1 if it fails */
If nCnn < 0
? "Connection error. See sqlerror.log for details."
Quit
EndIf
Return .T.
Esse exemplo acima e um dos exemplos do xharbour comercial+sqlrdd e nao apenas conecta ao mysql(nativo/odbc) mais tambem a todos os bancos de dados existente no mercado como postgres(nativo/odbc),firebird(nativo/odbc),interbase(nativo/odbc),oracle(nativo/odbc) ,mssql server(odbc) , dbc(odbc) e todos os demais bancos do mercado via odbc
para testarem esse programa, voce podem fazer o download do demo em www.xharbour.com.br
Atenciosamente
Luiz
lembro que vi uma mensagem de exemplo de xharbour+mysql
entao ai vai
#include "sqlrdd.ch"
#define RECORDS_IN_TEST 1000
#define SQL_DBMS_NAME 17
#define SQL_DBMS_VER 18
/*------------------------------------------------------------------------*/
Function Main( cDSN, lLog )
/* CODE_IS is the primary key 1st key. See SQLRDD.CH for details about structure array */
local aStruct := {{"CODE_ID","C",8,0,.F.,,,,,1 },{"CARDID","C",1,0},{"DESCR","C",50,0},{"PERCENT","N",10,2},{"DAYS","N",8,0},{"DATE_LIM","D",8,0},{"ENABLE","L",1,0},{"OBS","M",10,0}, {"VALUE","N",18,6}}
local nCnn, i, s
? ""
? "demo.exe"
? ""
? "Small SQLRDD demo"
? "(c) 2003 - Marcelo Lombardo"
? ""
Connect( cDSN ) // see connect.prg
? "Connected to :", SR_GetConnectionInfo(, SQL_DBMS_NAME ), SR_GetConnectionInfo(, SQL_DBMS_VER )
If lLog != NIL
? "Starting LOG", SR_GetActiveConnection(), SR_StartLog()
endif
// SR_SetSyntheticIndex(.t.)
? "Creating table :", dbCreate( "TEST_TABLE", aStruct, "SQLRDD" )
USE "TEST_TABLE" SHARED VIA "SQLRDD"
? "Table opened. Alias :", select(), alias(), RddName()
? "Fieldpos( CODE_ID ) :", Fieldpos( "CODE_ID" )
? "Fieldpos( DESCR ) :", Fieldpos( "DESCR" )
? "Creating 02 indexes..."
s := seconds()
Index on CODE_ID+DESCR to TEST_TABLE_IND01
Index on str(DAYS)+dtos(DATE_LIM) to TEST_TABLE_IND02
? "Done, Elapsed time :", seconds() - s, "seconds"
? ""
? "Appending " + alltrim(str(RECORDS_IN_TEST)) + " records.."
s := seconds()
For i = 1 to RECORDS_IN_TEST
Append Blank
Replace CODE_ID with strZero( i, 5 )
Replace DESCR with dtoc( date() ) + " - " + time()
Replace DAYS with (RECORDS_IN_TEST - i)
Replace DATE_LIM with date()
Replace ENABLE with .T.
Replace OBS with "This is a memo field. Seconds since midnight : " + alltrim(str(seconds()))
Next
? "Done, Elapsed time :", seconds() - s, "seconds"
? ""
? "dbClearIndex() :", dbClearIndex()
? "dbCloseArea() :", dbCloseArea()
? ""
? "Press any key to open the table"
inkey(0)
USE "TEST_TABLE" SHARED VIA "SQLRDD"
? "Opening Indexes"
SET INDEX TO TEST_TABLE_IND01
SET INDEX TO TEST_TABLE_IND02 ADDITIVE
? "Set Order to 1 :", OrdSetFocus(1)
? "Seek :", dbSeek( "00002" )
? "found() :", found()
? "Recno(),bof(),eof() :", recno(), bof(), eof()
? "dbUnLock() :", dbUnLock()
? "RLock(), dbRLockList:", rlock(), sr_showVector( dbRLockList() )
? "Writes to the WA :", FIELD->DESCR := "Hello, SQL!", FIELD->PERCENT := 23.55
? "dbCommit() :", dbCommit()
? " "
? "Press any key to browse()"
inkey(0)
clear
browse(row()+1,1,row()+20,80)
clear
? "Order 2, key is :", OrdSetFocus(2), ordKey()
OrdScope( 0, str(RECORDS_IN_TEST / 4,8) )
OrdScope( 1, str(RECORDS_IN_TEST / 2,8) )
? "TOP Scope :", OrdScope( 0 )
? "BOTTOM Scope :", OrdScope( 1 )
? "Press any key to browse() with another index and scope"
inkey(0)
dbGoTop()
clear
browse(row()+1,1,row()+20,80)
Return NIL
#include "pgs.ch" // Needed if you plan to use native connection to Postgres
#include "mysql.ch" // Needed if you plan to use native connection to MySQL
#include "oracle.ch" // Needed if you plan to use native connection to Oracle
#include "firebird.ch" // Needed if you plan to use native connection to Firebird
REQUEST SQLRDD // SQLRDD should be linked in
REQUEST SR_ODBC // Needed if you plan to connect with ODBC
REQUEST SR_PGS // Needed if you plan to use native connection to Postgres
REQUEST SR_MYSQL // Needed if you plan to use native connection to MySQL
REQUEST SR_ORACLE // Needed if you plan to use native connection to Oracle
REQUEST SR_FIREBIRD // Needed if you plan to use native connection to Firebird
REQUEST DBFNTX
REQUEST DBFCDX
REQUEST DBFFPT
REQUEST DBFDBT
/*------------------------------------------------------------------------*/
Function Connect( cDatabase )
local nCnn, nDrv, cDriver, nOpt, nDetected, hIniFile, aKeys, nKey, cConnString
local oldScreen, hDsn
hIniFile := HB_ReadIni( "sqlrdd.ini", .F.,,.F. ) // Read ini file in a hash table
If hIniFile == NIL
? "Could not read from sqlrdd.ini"
Quit
EndIf
If cDatabase == NIL
aKeys := HGetKeys( hIniFile )
If len(aKeys) == 0
? "No connections available in sqlrdd.ini"
Quit
ElseIf len(aKeys) == 1
nKey := 1
Else
clear screen
@5,1 say PadC( "Choose connection option", 80 )
nKey := achoice( 7, 20, 20, 60, aKeys )
clear screen
If nKey == 0
? "No connection selected"
Quit
EndIf
EndIf
hDsn := HGetValueAt( hIniFile, nKey )
If !"CONNSTRING" IN hDsn
? "ConnString not found in " + aKeys[nKey]
Quit
EndIf
Else
If ! cDatabase IN hIniFile
? "Connection [" + cDatabase + "] not found in sqlrdd.ini"
Quit
EndIf
hDsn := hIniFile[ cDatabase ]
If !"CONNSTRING" IN hDsn
? "ConnString not found in " + cDatabase
Quit
EndIf
EndIf
cConnString := hDsn[ "CONNSTRING" ]
nDetected := DetectDBFromDSN( cConnString )
If nDetected > SYSTEMID_UNKNOW
? "Connecting to", cConnString
nCnn := SR_AddConnection( nDetected, cConnString )
Else
clear screen
nOpt := Alert( "Please, select connection type", { "ODBC", "Postgres", "MySQL", "Oracle", "Firebird" } )
If nOpt > 0
nCnn := SR_AddConnection( If( nOpt = 1, CONNECT_ODBC, if( nOpt = 2, CONNECT_POSTGRES, if( nOpt = 3, CONNECT_MYSQL, if( nOpt = 4, CONNECT_ORACLE, CONNECT_FIREBIRD ) ) ) ), cConnString )
Else
? "No connection type selected"
Quit
EndIf
EndIf
/* returns the connection handle or -1 if it fails */
If nCnn < 0
? "Connection error. See sqlerror.log for details."
Quit
EndIf
Return .T.
Esse exemplo acima e um dos exemplos do xharbour comercial+sqlrdd e nao apenas conecta ao mysql(nativo/odbc) mais tambem a todos os bancos de dados existente no mercado como postgres(nativo/odbc),firebird(nativo/odbc),interbase(nativo/odbc),oracle(nativo/odbc) ,mssql server(odbc) , dbc(odbc) e todos os demais bancos do mercado via odbc
para testarem esse programa, voce podem fazer o download do demo em www.xharbour.com.br
Atenciosamente
Luiz