func_odbc in Asterisk

Sooner or later, when you develop a dialplan for asterisk you'll need to work with database. There are many approaches for this. Below there is an old post from my previous blog, describing how to accomplish this with func_odbc(). This information is a bit outdated - it was tested on Asterisk 1.4, so it may not be usable for the current versions.

Of course in order to use func_odbc() you should have installed and configured ODBC itself. If you don't know how to do this check this great article from Debian-Administration.org. I'll only add that if the database connections are described in /etc/odbc.ini they will be accessible from all users of the system, which might be unacceptable. Alternative approach is to save the configuration in ~/.odbc.ini so they will be accessible only for the given user.

In Asterisk, the configuration file res_odbc.conf should be modified. For example:

[asterisk]
enabled => yes
dsn => ASTERISK
username => user
password => pass
pre-connect => yes

The database name is specified in square brackets, dsn is the dsn from odbc.ini. After that in func_odbc.conf you should configure the desired requests. For example:

[TEST]
dsn=asterisk
read=SELECT name from test where extension='${ARG1}';
write=INSERT INTO test VALUES ('${ARG1}', '${VAL2}');

As with AstDB, each function can read or write from/to the database. Here is an example how to use the new functions in Asterisk dialplan:

Set(foo=${ODBC_TEST(${EXTEN})})
Set(ODBC_TEST(alpha)=200)

The first line gets a value from the database, the second - saves one. alpha and ${EXTEN} are arguments (ARG). 200 is value (VAL). There are more examples at voip-info.org

Comments

Comments powered by Disqus