Page 1 of 1

HIX Server connect to Mariadb or Mysql

Posted: Tue Aug 04, 2026 3:59 am
by dutch

Dear Carles,

How to connect to Mariadb or Mysql to fetch the data?

Thanks in advance,


Re: HIX Server connect to Mariadb or Mysql

Posted: Wed Aug 26, 2026 5:30 pm
by AiBot
To connect to MariaDB or MySQL from **HIX Server**, you typically use the **hbhmysql** (or **hbmysql**) RDD / SQL layer via **ADOSYS** (ADO-like layer for Harbour) or direct **SQLMIX** / **HBHMYSQL** function calls.

The standard modern approach in FiveTech environments is using **`TMySQLConnection`** (via `hbhmysql` library) or **ADOSYS** with a DSN-less connection string.

### 1. Prerequisites
* Ensure `hbhmysql.lib` (or `hbmysql.lib`) is linked in your HIX project (`.hbp` file).
* Copy `libmysql.dll` (or `libmariadb.dll`) into the same folder as your `hixserver.exe` (or system PATH).
* Add `REQUEST HBHMYSQL` (or `REQUEST HBMYSQL`) in your `main.prg` or `rddsys.prg`.

### 2. Connection Example (Direct SQL Layer / hbhmysql)

```harbour
#include "hbclass.ch"
#include "sqlmix.ch"

FUNCTION ConnectMySQL()
LOCAL oConn
LOCAL cConnString := "hbhmysql://user:pass@localhost:3306/dbname?charset=utf8mb4"

// Option A: Using SQLMIX (TMySQLConnection)
oConn := TMySQLConnection():New( cConnString )

IF oConn:IsConnected()
? "Connected OK"
// Execute queries
oConn:Execute( "SELECT VERSION()" )
// ...
oConn:Close()
ELSE
? "Error:", oConn:GetLastError()
ENDIF

RETURN oConn
```

### 3. Connection Example (ADOSYS - Recommended for portable code)

```harbour
#include "ado.ch"

FUNCTION ConnectADO()
LOCAL oConn := ADONew( "ADODB.Connection" )
LOCAL cConnStr := "DRIVER={MariaDB ODBC 3.1 Driver};SERVER=localhost;PORT=3306;DATABASE=dbname;USER=user;PASSWORD=pass;OPTION=3;CHARSET=utf8mb4;"

// Or DSN-less for MySQL native driver (if installed):
// cConnStr := "DRIVER={MySQL ODBC 8.0 Unicode Driver};SERVER=localhost;..."

TRY
oConn:Open( cConnStr )
? "ADO Connected OK"

// Example Query
LOCAL oRS := ADONew( "ADODB.Recordset" )
oRS:Open( "SELECT * FROM customers LIMIT 10", oConn )

WHILE !oRS:EOF
? oRS:Fields("name"):Value
oRS:MoveNext()
ENDDO
oRS:Close()

CATCH oErr
? "ADO Error:", oErr:Description
END

oConn:Close()
RETURN NIL
```

### 4. HIX Server Context (Thread Safety)
Since HIX Server is **multi-threaded** (one thread per request):
1. **Do not share a single connection object** across threads.
2. **Create the connection inside the request handler** (or use a connection pool pattern if load is high).
3. Always `oConn:Close()` (or `oConn:Destroy()`) before returning the response.

**Typical HIX Handler Structure:**

```harbour
FUNCTION MyApiHandler( oRequest, oResponse )
LOCAL oConn := ConnectMySQL() // Creates new connection per request

IF oConn != NIL .AND. oConn:IsConnected()
LOCAL aData := oConn:GetAll( "SELECT id, name FROM users" )
oResponse:SetJson( aData )
ELSE
oResponse:SetStatus( 500 )
oResponse:SetJson( { "error" : "DB Connection Failed" } )
ENDIF

IF oConn != NIL
oConn:Close()
ENDIF

RETURN .T.
```

### Summary Checklist
1. **Link library:** `hbhmysql` (native) or configure **ODBC** (ADOSYS).
2. **DLLs:** `libmariadb.dll` / `libmysql.dll` in exe folder.
3. **Charset:** Always specify `charset=utf8mb4` in connection string.
4. **Scope:** One connection per HIX request thread.

Which approach are you currently trying (Native `hbhmysql` or `ADOSYS`/ODBC)? I can detail the specific setup for that path.

[size=85][i]Respuesta generada por Inteligencia Artificial usando el modelo nemotron-3-ultra-free via OpenCode Zen. Puede contener errores; por favor verifiquela antes de aplicar.[/i][/size]
[size=85][i]AI-generated reply using the nemotron-3-ultra-free model via OpenCode Zen. May contain mistakes; please verify before applying.[/i][/size]

Re: HIX Server connect to Mariadb or Mysql

Posted: Thu Aug 27, 2026 7:04 am
by Carles

Dutch,

How are you currently connecting from Harbour?

Can you give a simple example?

C.


Re: HIX Server connect to Mariadb or Mysql

Posted: Tue Sep 01, 2026 1:55 am
by dutch

Dear Carles,

I use FWCONNECT to connect the Mariadb server, not a pure Harbour. In previously, I use TMySqlServer() classes.

Do I need to build HIX that will support Mariadb or MySql first?
If so, How can I make it?

Carles wrote: Thu Aug 27, 2026 7:04 am

Dutch,

How are you currently connecting from Harbour?

Can you give a simple example?

C.


Re: HIX Server connect to Mariadb or Mysql

Posted: Thu Sep 03, 2026 5:22 am
by Carles

Dutch,

Two ways to work.

  1. Download the binary from the releases section of GitHub and test if you have any standard Harbour functions to connect.

  2. If you don't have the function you're looking for (by default, the entire harbour.hbx file is loaded), download all the source code and first try compiling with the *.bat files (you'll need to adjust the paths first). If you manage to compile the server, simply add the desired source code to the hbp file and recompile.

The second method allows anyone to add functionalities they need, such as using ADS. You have access to all the source code.

Repository https://github.com/carles9000/hix

C.


Re: HIX Server connect to Mariadb or Mysql

Posted: Wed Sep 16, 2026 1:33 pm
by dutch

Deare Carles,

I can connect to Mariadb already with native TMySqlServer():New(cHost, cUser, cPassword, nPort) now.

Thank you so much,
Dutch

Carles wrote: Thu Sep 03, 2026 5:22 am

Dutch,

Two ways to work.

  1. Download the binary from the releases section of GitHub and test if you have any standard Harbour functions to connect.

  2. If you don't have the function you're looking for (by default, the entire harbour.hbx file is loaded), download all the source code and first try compiling with the *.bat files (you'll need to adjust the paths first). If you manage to compile the server, simply add the desired source code to the hbp file and recompile.

The second method allows anyone to add functionalities they need, such as using ADS. You have access to all the source code.

Repository https://github.com/carles9000/hix

C.