Re: [gnome-db]personal databases with gnome-db
- From: Rodrigo Moya <rodrigo gnome-db org>
- To: Christopher "B." Browne <cbbrowne hex net>
- Cc: GDA <gnome-db-list gnome org>
- Subject: Re: [gnome-db]personal databases with gnome-db
- Date: 15 Apr 2001 01:00:05 +0200
On 14 Apr 2001 17:34:48 -0500, Christopher B. Browne wrote:
>
> You might want to bounce this over to the mailing list; I'm not
> formally subscribed.
>
> rodrigo gnome-db org (Rodrigo Moya) writes:
> > well, the problem with Berkeley DB is that it only allows key->value
> > pairs to be stored, which makes it a bit difficult for a novice user
> > to have to distribute its data into several files.
>
> What you've said is partially inaccurate.
>
> It is baldly correct to say that:
>
> "Berkeley DB only allows key->value pairs to be stored."
>
> However. Berkeley DB allows multiple "virtual databases" to be stored
> in a single database, where you specify which database is to be
> accessed via the variable "database" in the call:
>
yes, I know that. What I was talking about is about the contents of each
virtual database (or table). That data is only key->value. And, as your
code example demonstrates, it is easy to simulate multiple fields in the
value part. But, also as your example shows, you MUST know in advance
the format of the data you stored there (you use \t for delimiters, but
how would I know that if I'm opening this file myself without prior
knowledge?). And, for a generic library, as is libgda, how could we
guess the format used in a specific DB database records?
If I'm wrong, please, correct me.
cheers
> dbp->open(dbp, DBFILE, database, DB_BTREE, DB_CREATE, 0664);
>
> The code below shows a gratuitously simple and largely useless example
> of this; it's pretty ugly code, but certainly demonstrates the point
> that you can simultaneously have a bunch of "data tables" in one file.
>
> ---- Set phasers to CUT HERE ----
> /* Testing out Berkeley DB */
> /* Christopher Browne - cbbrowne ntlug org */
> #include <stdlib.h>
> #include <stdio.h>
> #include <db.h>
> /* Specify multiple databases in DB->open (); */
> void read_data_into_db (char *filename, char *database);
> /* Data is stored in this DB file */
> #define DBFILE "/tmp/sample.db"
>
> DB_BTREE_STAT *statp;
>
> int main(int argc, char *argv[]) {
> int ret;
> DBC *dbcp;
> printf("Starting up...\n");
> /* unlink (DBFILE); */
> #define DB1 "password file"
> #define DB2 "hosts file"
> read_data_into_db( "/etc/passwd", "password file" );
> read_data_into_db( "/etc/hosts", "hosts file");
> read_data_into_db( "/etc/groups", "groups" );
> read_data_into_db( "/etc/inittab", "init" );
> }
>
> void read_data_into_db (char *filename, char *database) {
> DBT key, data;
> DB *dbp;
> int rc;
> char line[4096], *ret, *token, *cp, *rest;
> FILE *fp;
> const char delimiters[] = " \t:";
> printf("read_data_into_db\n");
> if ((fp = fopen(filename, "r")) == NULL) {
> printf ("Oops - couldn't open %s\n", filename);
> }
> printf("read_data_into_db - opened file\n");
> memset(&key, 0, sizeof(DBT));
> memset(&data, 0, sizeof(DBT));
> if ((ret = db_create(&dbp, NULL, 0)) != 0) {
> printf("Ooops - db_create failed - %s\n", db_strerror(ret));
> }
> if ( rc = dbp->open(dbp, DBFILE, database, DB_BTREE, DB_CREATE, 0664) != 0) {
> dbp->err (dbp, rc, "opening: %s", DBFILE);
> }
> printf("read_data_into_db - opened DB\n");
>
> do {
> ret = fgets(line, 255, fp);
> if ((ret != NULL) && (*ret != '#')) {
> cp = strdup (ret);
> token = strtok(cp, delimiters);
> rest = ret + (strlen(token));
> ret[strlen(ret) - 1] = (char) 0;
> printf("Read line: [%s] and [%s]\n", token, rest);
> key.data = token;
> key.size = strlen(token);
> data.data = rest;
> data.size = strlen(rest);
> if ((rc = dbp->put (dbp, NULL, &key, &data, 0)) != 0) {
> dbp->err(dbp, rc, "DB->put");
> }
> }
> } while (ret != NULL) ;
> (void) fclose(fp);
>
> dbp->stat(dbp, &statp, NULL, 0);
> printf("Database contains %lu records\n", (u_long) statp->bt_ndata);
> dbp->close(dbp, 0);
> printf("done\n");
> }
> --
> (reverse (concatenate 'string "gro.gultn@" "enworbbc"))
> http://www.ntlug.org/~cbbrowne/resume.html
> You have a tendency to feel you are superior to most computers.
>
--
Rodrigo Moya <rodrigo gnome-db org> <rodrigo ximian com>
http://www.gnome-db.org/
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]