Pages

Sunday, 27 March 2011

Creating Berkeley DBs and Adding Records

Since there is no GUI for Berkeley and no need for that(since its key-value pair oriented) we will be using a programming language to access its datas.I preferred creating a C#(Console) Application.

Im assuming you've already downloaded Berkeley DB from Oracle's website here

Lets create a new Console Application and then add reference to "libdb_dotnet51.dll" which is Oracle Berkeley DB's Managed Library for accessing Berkeley datas.

2.png

I've created a (.NET Framework 4.0) C# Console Application and named it "AccessingBerkeley"

Now Add Reference to this project and choose Browse tab and get to this location:

For 64-bit: C:\Program Files (x86)\Oracle\Berkeley DB 11gR2 5.1.25\bin
For 32-bit: C:\Program Files\Oracle\Berkeley DB 11gR2 5.1.25\bin

3.png

Choose libdb_dotnet51.dll.This is the Managed Library for Berkeley DB.

Now to introduce Berkeley Library in your code first you need to import its namespace to the project.

Use this code:


Dont forget to add a couple more namespaces:

using System;
using System.IO;
using System.Collections.Generic;
using System.Collections;
using System.Diagnostics;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using BerkeleyDB;

Then before creating our database and adding records you'll need to add some variables:

 BTreeDatabase btreeDB;
BTreeDatabaseConfig btreeConfig;
Cursor dbc;
DatabaseEntry key;
DatabaseEntry data;
string buff, dbFileName, keyString;

Now add these codes for creating database,checking if db exists and adding records:

  try
{
String pwd = Environment.CurrentDirectory;
pwd = Path.Combine(pwd, "..");
pwd = Path.Combine(pwd, "..");
if (IntPtr.Size == 4)
pwd = Path.Combine(pwd, "Win32");
else
pwd = Path.Combine(pwd, "x64");
#if DEBUG
pwd = Path.Combine(pwd, "Debug");
#else
pwd = Path.Combine(pwd, "Release");
#endif
pwd += ";" + Environment.GetEnvironmentVariable("PATH");
Environment.SetEnvironmentVariable("PATH", pwd);
}
catch (Exception e)
{
Console.WriteLine(
"Unable to set the PATH environment variable.");
Console.WriteLine(e.Message);
return;
}

try
{
dbFileName = "access.db";
if (args.Length > 0)
dbFileName = args[0];
}
catch
{
usage();
return;
}

if (File.Exists(dbFileName))
{
while (true)
{
Console.Write
("{0} already exists. Delete it? (y/n) ", dbFileName);
buff = Console.ReadLine().ToLower();
if (buff == "y" || buff == "n")
break;
}

if (buff == "y")
{
try
{
File.Delete(dbFileName);
}
catch
{
Console.WriteLine("Unable to delete {0}.", dbFileName);
return;
}
}
}

btreeConfig = new BTreeDatabaseConfig();
btreeConfig.Duplicates = DuplicatesPolicy.SORTED;
btreeConfig.ErrorPrefix = "excs_access";
btreeConfig.Creation = CreatePolicy.IF_NEEDED;
btreeConfig.CacheSize = new CacheInfo(0, 64 * 1024, 1);
btreeConfig.PageSize = 8 * 1024;

try
{
btreeDB = BTreeDatabase.Open(dbFileName, btreeConfig);
}
catch (Exception e)
{
Console.WriteLine("Error opening {0}.", dbFileName);
Console.WriteLine(e.Message);
return;
}

key = new DatabaseEntry();
data = new DatabaseEntry();

while (true)
{
Console.Write("key [blank line to quit] > ");
keyString = Console.ReadLine();
if (keyString == "")
break;

dbtFromString(key, keyString);
dbtFromString(data, reverse(keyString));

try
{
btreeDB.Put(key, data);
}
catch
{
return;
}

}

using (dbc = btreeDB.Cursor())
{

Console.WriteLine("All key : data pairs:");
foreach (KeyValuePair p in dbc)
Console.WriteLine("{0}::{1}",
strFromDBT(p.Key), strFromDBT(p.Value));
}

Console.Write("Press any key to exit >");
Console.ReadKey(true);

btreeDB.Close();
}

And dont forget to add these utility methods that help it make readable.

 public static void usage()
{
Console.WriteLine(
"Usage: excs_access [database]");
}

static void dbtFromString(DatabaseEntry dbt, string s)
{
dbt.Data = System.Text.Encoding.ASCII.GetBytes(s);
}

public static string strFromDBT(DatabaseEntry dbt)
{

System.Text.ASCIIEncoding decode =
new ASCIIEncoding();
return decode.GetString(dbt.Data);
}

public static string reverse(string s)
{
StringBuilder tmp = new StringBuilder(s.Length);
for (int i = s.Length - 1; i >= 0; i--)
tmp.Append(s[i]);
return tmp.ToString();
}


After you run the project it will be creating a "access.db" in Debug folder(same location with your exe file)

Now run the project and see the results.It will wait you finish adding records until you press "Enter" key.

4.png

After you click Enter it will list all the datas with its reversed strings as values.

5.png

Now press Enter and exit application.Then re-run the application:

6.png

You'll be displaying a confirmation message informing you that access.db is about to be deleted.You could write "y" or "n" to confirm.

If you write "n" you can view your previously added datas:

7.png

Thats how we did it.


View the original article here

1 comments:

Anonymous said...

Hi how to change the default db file directory to something link c:\db ?

Post a Comment

 
Powered by Blogger