#include "hbdyn.ch"
#include "common.ch"
#define MYSQL_CLIENTLIB "libmysql.dll"
//#define MYSQL_CLIENTLIB "/usr/lib/x86_64-linux-gnu/libmysqlclient.so.20"
#define CRLF hb_OsNewLine()
STATIC pLib
function Main()
local hMySql, hMyRes
local nErr, cErr
local nResStatus
local cSql
local nRows, nFields
cSql := "SELECT num_roc, fecha, cliente FROM roc"
setmode(25,80)
cls
hMySql := mysql_real_connect( "192.168.10.10", "myusuario", "myclave123", "mybasededatos", 3306, 0 )
?"pLib =", pLib, MyType( pLib )
?"hMySql =", hb_ntos( hMySql ), MyType( hMySql )
IF mysql_errno( hMySql ) == 0
?"Version: " + mysql_get_server_info( hMySql )
?"Sql: " + cSql
nResStatus := mysql_real_query( hMySql, cSql )
IF nResStatus == 0
hMyRes := mysql_store_result( hMySql )
?"hMyRes= ", hb_ntos( hMyRes ), MyType( hMyRes )
nRows := mysql_num_rows( hMyRes )
nFields := mysql_num_fields( hMyRes )
?"Rows: ", hb_ntos( nRows ), MyType( nRows )
?"Fields: ", hb_ntos( nFields ), MyType( nFields )
?"-----------------------"
?mysql_fetch_field( hMyRes )
ELSE
?"ErrNo: ", hb_ntos( mysql_errno( hMySql ) ), "Error: ", mysql_error( hMySql )
ENDIF
ELSE
?"ErrNo: ", hb_ntos( mysql_errno( hMySql ) ), "Error: ", mysql_error( hMySql )
ENDIF
mysql_close( hMySql )
hb_LibFree( pLib )
return nil
/*------------------------------------------------------------*/
function MyType( v )
return "[" + valtype( v ) + "]"
/*------------------------------------------------------------*/
function mysql_real_connect( cHost, cUser, cPass, cDatabase, nPort, nFlag )
local hMySql
pLib := hb_LibLoad( MYSQL_CLIENTLIB )
hMySql := hb_DynCall( { "mysql_init", pLib, HB_DYN_CALLCONV_CDECL }, NIL )
if !empty( hMySql )
hb_DynCall( { "mysql_real_connect", pLib, HB_DYN_CALLCONV_CDECL, ;
HB_DYN_CTYPE_LONG_UNSIGNED, ; /*MYSQL* connection handler*/
HB_DYN_CTYPE_CHAR_PTR , ; /*const char *host*/
HB_DYN_CTYPE_CHAR_PTR , ; /*const char *user*/
HB_DYN_CTYPE_CHAR_PTR , ; /*const char *passwd*/
HB_DYN_CTYPE_CHAR_PTR , ; /*const char *db*/
HB_DYN_CTYPE_LONG , ; /*unsigned int port*/
HB_DYN_CTYPE_CHAR_PTR , ; /*const char *unix_socket*/
HB_DYN_CTYPE_LONG } , ; /*unsigned long client_flag*/
hMySql, cHost, cUser, cPass, cDatabase, nPort, NIL, nFlag )
endif
return hMySql
/*------------------------------------------------------------*/
function mysql_close( hMySql )
if !empty( pLib )
hb_DynCall( { "mysql_close", pLib, HB_DYN_CALLCONV_CDECL, ;
HB_DYN_CTYPE_LONG_UNSIGNED }, hMySql )
endif
return nil
/*------------------------------------------------------------*/
function mysql_errno( hMySql )
local nError := 0
if !empty( pLib )
nError := hb_DynCall( { "mysql_errno", pLib, HB_DYN_CALLCONV_CDECL, ;
HB_DYN_CTYPE_LONG_UNSIGNED }, hMySql )
endif
return nError
/*------------------------------------------------------------*/
function mysql_error( hMySql )
local cError := ""
if !empty( pLib )
cError := hb_DynCall( { "mysql_error", pLib, hb_bitOr( HB_DYN_CTYPE_CHAR_PTR, HB_DYN_CALLCONV_CDECL ), ;
HB_DYN_CTYPE_LONG_UNSIGNED }, hMySql )
endif
return cError
/*------------------------------------------------------------*/
function mysql_real_query( hMySql, cQuery )
local nRet
local nLen := len(cQuery)
if !empty( pLib )
nRet := hb_DynCall( { "mysql_real_query", pLib, HB_DYN_CALLCONV_CDECL, ;
HB_DYN_CTYPE_LONG_UNSIGNED , ; /*MYSQL *mysql*/
HB_DYN_CTYPE_CHAR_PTR , ; /*const char *stmt_str*/
HB_DYN_CTYPE_LONG_UNSIGNED }, ; /*unsigned long length*/
hMySql, cQuery, nLen )
endif
return nRet
/*------------------------------------------------------------*/
function mysql_store_result( hMySql )
local hMyRes
if !empty( pLib )
hMyRes := hb_DynCall( { "mysql_store_result", pLib, HB_DYN_CALLCONV_CDECL, ;
HB_DYN_CTYPE_LONG_UNSIGNED }, hMySql )
endif
return hMyRes
/*------------------------------------------------------------*/
function mysql_free_result( hMyRes )
if !empty( pLib )
hb_DynCall( { "mysql_free_result", pLib, HB_DYN_CALLCONV_CDECL, ;
HB_DYN_CTYPE_LONG_UNSIGNED }, hMyRes )
endif
return nil
/*------------------------------------------------------------*/
function mysql_get_server_info( hMySql )
local cVersion := ""
if !empty( pLib )
cVersion := hb_DynCall( { "mysql_get_server_info", pLib, hb_bitOr( HB_DYN_CTYPE_CHAR_PTR, HB_DYN_CALLCONV_CDECL ), ;
HB_DYN_CTYPE_LONG_UNSIGNED }, hMySql )
endif
return cVersion
/*------------------------------------------------------------*/
function mysql_num_rows( hMyRes )
local nRows := 0
if !empty( pLib ) .and. !empty( hMyRes )
nRows := hb_DynCall( { "mysql_num_rows", pLib, hb_bitOr( HB_DYN_CALLCONV_CDECL, HB_DYN_CTYPE_LLONG_UNSIGNED ), ;
HB_DYN_CTYPE_LLONG_UNSIGNED }, hMyRes )
endif
return nRows
/*------------------------------------------------------------*/
function mysql_num_fields( hMyRes )
local nFields := 0
if !empty( pLib ) .and. !empty( hMyRes )
nFields := hb_DynCall( { "mysql_num_fields", pLib, hb_bitOr( HB_DYN_CALLCONV_CDECL, HB_DYN_CTYPE_INT_UNSIGNED ), ;
HB_DYN_CTYPE_INT_UNSIGNED }, hMyRes )
endif
return nFields
/*------------------------------------------------------------*/
function mysql_fetch_field( hMyRes )
local hField
if !empty( pLib ) .and. !empty( hMyRes )
hField := hb_DynCall( { "mysql_fetch_field", pLib, hb_bitor( HB_DYN_CALLCONV_CDECL, HB_DYN_CTYPE_VOID_PTR ), ;
HB_DYN_CTYPE_LONG_UNSIGNED }, hMyRes )
endif
return hField
/*------------------------------------------------------------*/
function My_FetchRow( hMyRes )
local hRow
if !empty( pLib ) .and. !empty( hMyRes )
hRow := hb_DynCall( { "mysql_fetch_row", pLib, HB_DYN_CALLCONV_CDECL, ;
HB_DYN_CTYPE_LONG_UNSIGNED }, hMyRes )
endif
return hRow
/*------------------------------------------------------------*/
#pragma BEGINDUMP
#include "hbapi.h"
HB_FUNC( PTRTOSTR )
{
const char ** pStrs = (const char **) hb_parnll(1);
hb_retc( * ( pStrs + hb_parnl( 2 ) ) );
}
#pragma ENDDUMP