Mariadb Database read and write

I am using astersik 20.8.1.

I am trying to read and write values ​​from the database through agi in dialplan.

How do I access the database in agi?

The program uses C.
You can access the database by compiling the source code below with gcc.
When accessing AGI commands in extensions.conf, database-related commands cannot be executed…

I would like to ask for help with a simple example or reference on whether I need to use a different command on agi.

c The implemented source is as follows.

#include <mysql.h>
#include <stdio.h>
#include <stdlib.h>

void finish_with_error(MYSQL *con)
{
fprintf(stderr, “%s\n”, mysql_error(con));
mysql_close(con);
exit(1);
}

int main(int argc, char **argv)
{
MYSQL *con = mysql_init(NULL);

if (con == NULL)
{
fprintf(stderr, “mysql_init() failed\n”);
exit(1);
}

if (mysql_real_connect(con, “localhost”, “root”, “abcde”,
“testdb”, 0, NULL, 0) == NULL)
{
finish_with_error(con);
}

if (mysql_query(con, “SELECT * FROM cars LIMIT 3”))
{
finish_with_error(con);
}

MYSQL_RES *result = mysql_store_result(con);

if (result == NULL)
{
finish_with_error(con);
}

int num_fields = mysql_num_fields(result);

MYSQL_ROW row;
MYSQL_FIELD *field;

while ((row = mysql_fetch_row(result)))
{
for(int i = 0; i < num_fields; i++)
{
if (i == 0)
{
while(field = mysql_fetch_field(result))
{
printf("%s ", field->name);
}

         printf("\n");
      }

      printf("%s  ", row[i] ? row[i] : "NULL");
  }

}

printf(“\n”);

mysql_free_result(result);
mysql_close(con);

exit(0);
}

Trying to do it in a lower-level language like C is just making extra work for yourself. C may be faster at CPU-intensive things, but unless you have a demonstrated need for this, I would recommend using a higher-level language, like Python.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.