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)
### 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]
Download the binary from the releases section of GitHub and test if you have any standard Harbour functions to connect.
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.
Download the binary from the releases section of GitHub and test if you have any standard Harbour functions to connect.
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.