You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When you need to run a query with a where clause using multiple parameters, such as `WHERE col1='a' AND (col2='b' OR col3=5) AND col4=42.3`, you can use the operator `raw`:
289
+
290
+
```JSON
291
+
{
292
+
"table": "table1",
293
+
"columns": ["col1", "col2"]
294
+
"where": {
295
+
" ": {
296
+
"operator", "raw",
297
+
"value", "col1='a' AND (col2='b' or col3='c') AND col4=5"
298
+
}
299
+
}
300
+
}
301
+
```
302
+
303
+
In some cases, you may need to build the where clause with multiple variables. In hoel 1.4.27, the function `h_build_where_clause` was introduced to help that. Please note that this function is still in Beta.
304
+
305
+
```C
306
+
/**
307
+
* h_build_where_clause
308
+
* Generates a where clause based on the pattern and the values given
309
+
* @param conn the connection to the database
310
+
* @param pattern the pattern to build the where clause
311
+
* the pattern variables available are the following:
312
+
* - %s: a string value to escape with quotes
313
+
* - %S: a string value to escape without quotes
314
+
* - %c: a string value not to escape with quotes
315
+
* - %C: a string value not to escape without quotes
316
+
* - %d: an integer value in json_int_t format
317
+
* - %f: a double value
318
+
* - %j: a json_t value, the value must be of the type JSON_INTEGER, JSON_REAL or JSON_STRING, string values will be escaped with quotes
Note that if you use constant litteral for integer or double values, you should cast them first:
347
+
348
+
```C
349
+
constchar col1[] = "a", col2[] = "b";
350
+
char * where_clause = h_build_where_clause("col1=%s AND (col2='S' OR col3=%d) AND col4=%f", col1, col2, (json_int_t)5, (double)42.3);
351
+
```
352
+
286
353
### Execute a SQL query
287
354
288
355
To execute a SQL query, you can use the function `h_execute_query` which will run the query in the database specified by the parameter `conn`. If a `result` parameter is specified, the result of the query (if any) will be stored in the `result` structure.
0 commit comments