Using the Asterisk Database (AstDB)

Having fun yet? It gets even better!

Asterisk provides a powerful mechanism for storing values, called the Asterisk database (AstDB). The AstDB provides a simple way to store data for use within your dialplan.

Tip

For those of you with experience using relational databases such as PostgreSQL or MySQL, the Asterisk database is not a traditional relational database. It is a Berkeley DB Version 1 database . There are several ways to store data from Asterisk in a relational database, but this book will not delve into them.

The Asterisk database stores its data in groupings called families, with valuesidentified by keys. Within a family, a key may be used only once. For example, if we had a family called test, we could store only one value with a key called count. Each stored value must be associated with a family.

Storing Data in the AstDB

To store a new value in the Asterisk database, we use the Set() application,[43] but instead of using it to set a channel variable, we use it to set an AstDB variable. For example, to assign the count key in the test family the value of 1:

    exten => 456,1,Set(${DB(test/count)=1})

If a key named count already exists in the test family, its value will be overwritten with the new value. You can also store values from the Asterisk command line, by running the command database put family key value . For our example, you would type database put test count 1.

Retrieving Data from the AstDB

To retrieve a value from the Asterisk database ...

Get Asterisk: The Future of Telephony now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.