How to Create a db4 Database from a Text File or Array

By | July 21, 2008

When you need to deal with big amounts of data, MySQL is not a good solution. db4 is much faster and easier – you don’t need any SQL queries to extract the necessary values and use them. The process of db4 file creation is quite easy; you need to have your PHP compiled with dba support. If you don’t know how to do it, you can find it on my blog: dba support for PHP

If you have a text file, you need to have your values arranged. The best way to do this is to start each key-value pair with a new line. This way you will easily explode your strings into little arrays and then add them to your db4 database. The code is here:

// Let’s create our db4 file – you should have all the necessary permissions to do it.
$id=dba_open(‘./baza.db’, ‘c’, ‘cdb’);
// Opening for writing
$id=dba_open(‘./baza.db’, ‘w’, ‘db4’);
// Here is an array with keys and values (you may get it from your text file or using any other

// convenient way for you
$positions=array(“some key”=>”some value”);
// We’re inserting these values into our db4 database.
foreach ($positions as $key=>$value)
dba_insert($key, $value, $id);
// Optimizing our database file
// That’s all. We can close it


We have created a db4 file that should have bigger size than the initial txt file. Next tim I will show you how to access your db4 file and how to extract values from it.