Esta es una pequeña clase para acceder a Mysql desde FWPCC usando la
libreria MysqlMovile
- Code: Select all Expand view RUN
/*
* Fichero: tmysql.prg
* Descripcion: Clase para acceder a Mysql desde FWPCC
* Autor: Pablo Alberto Vidal
* Fecha: 26/10/2009
*/
#include "FWCE.ch"
Class tMySql
Data cHost Init "localhost"
Data nPuerto Init 3306
Data cUsuario Init "root"
Data cPassword Init ""
Data cDataBase Init "test"
Method Conectar()
Method MsQuery( cQuery ) // Ejecuta un sentencia y devuelve un Array con los datos
Method MsCommand( cQuery ) // Ejecuta un comando SQL y devuelve .T. o .f.
Method End() InLine MySQL_Disconnect()
EndClass
// Conectar a la Base de Datos
Method Conectar() Class tMysql
Local lOk := .F.
If ( MySQL_Connect( ::cHost, ::nPuerto, ::cUsuario, ::cPassword ) == 0 )
If ( MySQL_Select_DB( ::cDataBase ) == 0 )
lOk := .T.
EndIf
EndIf
Return( lOk )
// Ejecuta un comando
Method MsCommand( cQuery )
Local lOk := .F.
If( MySQL_Execute_Query( cQuery ) == 0 )
lOk := .T.
EndIf
Return( lOk )
//Devuelve un array de datos
Method MsQuery( cQuery )
Local aData := {}
Local nField := 0
Local nRow := 0
Local cFieldValue := ""
Local nRet := 0
nRet := MySQL_Execute_Query( cQuery )
If nRet != 0
MsgInfo( MySQL_Get_Last_Error( nRet ) )
Return( {} )
endif
aData := Array( MySQL_Get_RowCount() )
For nRow := 1 to MySQL_Get_RowCount()
aData[nRow] := Array( MySQL_Get_FieldCount() )
For nField := 1 to MySQL_Get_FieldCount()
nRet := MySQL_Get_Data( @cFieldValue, nRow, nField )
If( nRet != 0, MsgInfo( MySQL_Get_Last_Error( nRet ) ),;
aData[nRow][nField] := AllTrim( Upper( cFieldValue ) ) )
Next
Next
Return( aData )
// Codigo C tomado del foro y Escrito por
// Antonio Linares
#pragma BEGINDUMP
// field parameters for mysql_get_field_param() function
#define FIELD_PARAM_CATALOG 1
#define FIELD_PARAM_DB 2
#define FIELD_PARAM_TABLE 3
#define FIELD_PARAM_ORIGTABLE 4
#define FIELD_PARAM_FIELD 5
#define FIELD_PARAM_ORIGFIELD 6
#define FIELD_PARAM_LENGTH 7
#define FIELD_PARAM_TYPE 8
#define FIELD_PARAM_FLAGS 9
#define FIELD_PARAM_DECIMALS 10
int mysql_connect( char * host, int iPort, char * pszUsername, char * pszPassword );
void mysql_get_last_error( int nErrorCode, char * pszError );
int mysql_disconnect( void );
int mysql_select_db( char * pszDatabaseName );
int mysql_execute_query( char * pszQueryString );
int mysql_get_fieldcount( void );
int mysql_get_field( char * char_val, int * int_val, int nParam, int nIndex );
int mysql_get_rowcount( void );
int mysql_get_data( char * pszElem, int nRow, int nColumn );
#include <hbapi.h>
HB_FUNC( MYSQL_CONNECT ) // cHost, nPort, cUserName, cPassword
{
hb_retnl( mysql_connect( hb_parc( 1 ), hb_parnl( 2 ), hb_parc( 3 ), hb_parc( 4 ) ) );
}
HB_FUNC( MYSQL_GET_LAST_ERROR ) // nCode --> cError
{
char error[ 255 ];
mysql_get_last_error( hb_parnl( 1 ), error );
hb_retc( error );
}
HB_FUNC( MYSQL_DISCONNECT )
{
mysql_disconnect();
}
HB_FUNC( MYSQL_SELECT_DB ) // cDataBaseName --> nRet
{
hb_retnl( mysql_select_db( hb_parc( 1 ) ) );
}
HB_FUNC( MYSQL_EXECUTE_QUERY ) // cSQLQuery --> nRet
{
hb_retnl( mysql_execute_query( hb_parc( 1 ) ) );
}
HB_FUNC( MYSQL_GET_FIELDCOUNT ) // --> nFields
{
hb_retnl( mysql_get_fieldcount() );
}
HB_FUNC( MYSQL_GET_FIELD ) // nField, @cName --> nRet
{
char fieldname[ 255 ];
int int_buffer;
hb_retnl( mysql_get_field( fieldname, &int_buffer, FIELD_PARAM_FIELD, hb_parnl( 1 ) - 1 ) );
hb_storc( fieldname, 2 );
}
HB_FUNC( MYSQL_GET_ROWCOUNT ) // --> nRows
{
hb_retnl( mysql_get_rowcount() );
}
HB_FUNC( MYSQL_GET_DATA ) // @cFieldData, nRow, nField --> nRet
{
char buffer[ 8192 ]; // max data size = 8K
hb_retnl( mysql_get_data( buffer, hb_parnl( 2 ) - 1, hb_parnl( 3 ) - 1 ) );
hb_storc( buffer, 1 );
}
#pragma ENDDUMP