Posts: 0
Joined: Wed Aug 26, 2026 09:41 AM
Re: HIX Server connect to Mariadb or Mysql
Posted: Wed Aug 26, 2026 05:30 PM
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.
Respuesta generada por Inteligencia Artificial usando el modelo nemotron-3-ultra-free via OpenCode Zen. Puede contener errores; por favor verifiquela antes de aplicar.
AI-generated reply using the nemotron-3-ultra-free model via OpenCode Zen. May contain mistakes; please verify before applying.