Index: vtcross/trunk/src/cbr.c
===================================================================
--- vtcross/trunk/src/cbr.c	(revision 17)
+++ vtcross/trunk/src/cbr.c	(revision 18)
@@ -17,33 +17,91 @@
 };
 
-int cbr_callback(void *notUsed, int argc, char **argv, char **azColName);
+
+// open database or create database is it does not exist
+int OpenDatabase(cbr _cbr){
+    int rc;
+    //sqlite3 **db;
+
+    printf("database name: %s\n", _cbr->filename);
+    //rc = sqlite3_open(_cbr->filename, db);
+    rc = sqlite3_open(_cbr->filename, &(_cbr->db));
+    if (rc) {
+        //fprintf(stderr, "can't open database: %s\n", sqlite3_errmsg(*db));
+        fprintf(stderr, "can't open database: %s\n", sqlite3_errmsg(_cbr->db));
+        sqlite3_close(_cbr->db);
+        exit(1);
+    } else{
+        printf("database opened.\n");
+    }
+    //_cbr->db = *db;
+    return rc;
+}
+
+
+//int cbr_callback(void *notUsed, int argc, char **argv, char **azColName)
+int callback(void *notUsed, int argc, char **argv, char **azColName){
+    int i;
+    for(i=0; i<argc; i++){
+        printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
+    }
+    printf("\n");
+    return 0;
+}
+
+
+int ExecuteCommand(cbr _cbr){
+    int rc;
+    char *zErrMsg = 0;
+
+    printf("command: %s\n", _cbr->command);
+    rc = sqlite3_exec(_cbr->db, _cbr->command, callback, 0, &zErrMsg);
+    if( rc!=SQLITE_OK){
+        fprintf(stderr, "SQL error: %s\n", zErrMsg);
+        sqlite3_free(zErrMsg);
+    } else{
+        printf("command executed.\n");
+    }
+    return rc;
+}
+
 
 cbr cbr_create(char * _filename, char * _tablename, char * _cols[], unsigned int _len)
 {
+    // cbr is a pointer to struct cbr_s
     cbr _cbr = (cbr) malloc(sizeof(struct cbr_s));
 
-    // copy filename, tablename
+    // create database
+
+    // copy filename
+    unsigned int i=0;
     strcpy(_cbr->filename, _filename);
+
+    // execute create database command
+    // database handle
+    //_cbr->db = NULL;
+    OpenDatabase(_cbr);
+
+    // create table
+
+    // copy tablename
     strcpy(_cbr->tablename, _tablename);
 
-    _cbr->db = NULL;
+    // number of columns in the table
     _cbr->num_columns = _len;
 
-    // create table
+    // generate command
     strcpy(_cbr->command, "CREATE TABLE ");
-    strcat(_cbr->command, _tablename);
+    strcat(_cbr->command, _cbr->tablename);
     strcat(_cbr->command, "(");
-    unsigned int i;
-    for (i=0; i<_len; i++) {
+    for (i=0; i<_cbr->num_columns; i++) {
         strcat(_cbr->command, _cols[i]);
         strcat(_cbr->command, " FLOAT");
-        if (i != _len-1)
+        if (i != _cbr->num_columns-1) // not last entry
             strcat(_cbr->command, ", ");
     }
     strcat(_cbr->command, ");");
-    printf("command : %s\n", _cbr->command);
 
-    // execute command
-
+    // execute create table command
+    ExecuteCommand(_cbr);
 
     return _cbr;
@@ -53,4 +111,16 @@
 {
     // delete table here
+    // delete table
+    
+    /*// generate command, delete all entries of a table
+    strcpy(_cbr->command, "delete from ");
+    strcat(_cbr->command, _cbr->tablename);*/
+
+    // generate command, remove a table with its content
+    strcpy(_cbr->command, "drop table ");
+    strcat(_cbr->command, _cbr->tablename);
+
+    // execute delete command
+    ExecuteCommand(_cbr);
 
     free(_cbr);
@@ -59,5 +129,12 @@
 void cbr_print(cbr _cbr)
 {
-    printf("database %s, table %s:\n", _cbr->filename, _cbr->tablename);
+    // generate command
+    strcpy(_cbr->command, "select * from ");
+    strcat(_cbr->command, _cbr->tablename);
+    strcat(_cbr->command, ";");
+
+    // execute print (select all)  command
+    ExecuteCommand(_cbr);
+    //printf("database %s, table %s:\n", _cbr->filename, _cbr->tablename);
 }
 
@@ -81,4 +158,32 @@
 int cbr_add_row(cbr _cbr, char *_cols[], float *_vals, unsigned int _len)
 {
+    unsigned int i;
+    
+    // generate command
+    printf("%s\n", _cbr->command);
+    strcpy(_cbr->command, "insert into ");
+    strcat(_cbr->command, _cbr->tablename);
+    
+    strcat(_cbr->command, " (");
+    for (i=0; i<_len; i++) {
+        strcat(_cbr->command, _cols[i]);
+        if (i != _cbr->num_columns-1) // not last entry
+            strcat(_cbr->command, ", ");
+    }
+    strcat(_cbr->command, ") ");
+
+    strcat(_cbr->command, " values(");
+    for (i=0; i<_len; i++) {
+        // ???? how to fill the values if _cbr->num_columns != _len
+        // assume = in the following
+        sprintf(_cbr->command, "%s%f", _cbr->command, _vals[i]);
+        if (i != _cbr->num_columns-1) // not last entry
+            strcat(_cbr->command, ", ");
+    }
+    strcat(_cbr->command, ");");
+    
+    // execute add command
+    ExecuteCommand(_cbr);
+
     return 0;
 }
