Changeset 18 for vtcross/trunk

Show
Ignore:
Timestamp:
10/16/08 18:58:04 (16 years ago)
Author:
ahe
Message:

update cbr_create, cbr_free, cbr_print, cbr_add_row in cbr.c

Location:
vtcross/trunk/src
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • vtcross/trunk/src/cbr.c

    r17 r18  
    1717}; 
    1818 
    19 int cbr_callback(void *notUsed, int argc, char **argv, char **azColName); 
     19 
     20// open database or create database is it does not exist 
     21int OpenDatabase(cbr _cbr){ 
     22    int rc; 
     23    //sqlite3 **db; 
     24 
     25    printf("database name: %s\n", _cbr->filename); 
     26    //rc = sqlite3_open(_cbr->filename, db); 
     27    rc = sqlite3_open(_cbr->filename, &(_cbr->db)); 
     28    if (rc) { 
     29        //fprintf(stderr, "can't open database: %s\n", sqlite3_errmsg(*db)); 
     30        fprintf(stderr, "can't open database: %s\n", sqlite3_errmsg(_cbr->db)); 
     31        sqlite3_close(_cbr->db); 
     32        exit(1); 
     33    } else{ 
     34        printf("database opened.\n"); 
     35    } 
     36    //_cbr->db = *db; 
     37    return rc; 
     38} 
     39 
     40 
     41//int cbr_callback(void *notUsed, int argc, char **argv, char **azColName) 
     42int callback(void *notUsed, int argc, char **argv, char **azColName){ 
     43    int i; 
     44    for(i=0; i<argc; i++){ 
     45        printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); 
     46    } 
     47    printf("\n"); 
     48    return 0; 
     49} 
     50 
     51 
     52int ExecuteCommand(cbr _cbr){ 
     53    int rc; 
     54    char *zErrMsg = 0; 
     55 
     56    printf("command: %s\n", _cbr->command); 
     57    rc = sqlite3_exec(_cbr->db, _cbr->command, callback, 0, &zErrMsg); 
     58    if( rc!=SQLITE_OK){ 
     59        fprintf(stderr, "SQL error: %s\n", zErrMsg); 
     60        sqlite3_free(zErrMsg); 
     61    } else{ 
     62        printf("command executed.\n"); 
     63    } 
     64    return rc; 
     65} 
     66 
    2067 
    2168cbr cbr_create(char * _filename, char * _tablename, char * _cols[], unsigned int _len) 
    2269{ 
     70    // cbr is a pointer to struct cbr_s 
    2371    cbr _cbr = (cbr) malloc(sizeof(struct cbr_s)); 
    2472 
    25     // copy filename, tablename 
     73    // create database 
     74 
     75    // copy filename 
     76    unsigned int i=0; 
    2677    strcpy(_cbr->filename, _filename); 
     78 
     79    // execute create database command 
     80    // database handle 
     81    //_cbr->db = NULL; 
     82    OpenDatabase(_cbr); 
     83 
     84    // create table 
     85 
     86    // copy tablename 
    2787    strcpy(_cbr->tablename, _tablename); 
    2888 
    29     _cbr->db = NULL; 
     89    // number of columns in the table 
    3090    _cbr->num_columns = _len; 
    3191 
    32     // create table 
     92    // generate command 
    3393    strcpy(_cbr->command, "CREATE TABLE "); 
    34     strcat(_cbr->command, _tablename); 
     94    strcat(_cbr->command, _cbr->tablename); 
    3595    strcat(_cbr->command, "("); 
    36     unsigned int i; 
    37     for (i=0; i<_len; i++) { 
     96    for (i=0; i<_cbr->num_columns; i++) { 
    3897        strcat(_cbr->command, _cols[i]); 
    3998        strcat(_cbr->command, " FLOAT"); 
    40         if (i != _len-1) 
     99        if (i != _cbr->num_columns-1) // not last entry 
    41100            strcat(_cbr->command, ", "); 
    42101    } 
    43102    strcat(_cbr->command, ");"); 
    44     printf("command : %s\n", _cbr->command); 
    45103 
    46     // execute command 
    47  
     104    // execute create table command 
     105    ExecuteCommand(_cbr); 
    48106 
    49107    return _cbr; 
     
    53111{ 
    54112    // delete table here 
     113    // delete table 
     114     
     115    /*// generate command, delete all entries of a table 
     116    strcpy(_cbr->command, "delete from "); 
     117    strcat(_cbr->command, _cbr->tablename);*/ 
     118 
     119    // generate command, remove a table with its content 
     120    strcpy(_cbr->command, "drop table "); 
     121    strcat(_cbr->command, _cbr->tablename); 
     122 
     123    // execute delete command 
     124    ExecuteCommand(_cbr); 
    55125 
    56126    free(_cbr); 
     
    59129void cbr_print(cbr _cbr) 
    60130{ 
    61     printf("database %s, table %s:\n", _cbr->filename, _cbr->tablename); 
     131    // generate command 
     132    strcpy(_cbr->command, "select * from "); 
     133    strcat(_cbr->command, _cbr->tablename); 
     134    strcat(_cbr->command, ";"); 
     135 
     136    // execute print (select all)  command 
     137    ExecuteCommand(_cbr); 
     138    //printf("database %s, table %s:\n", _cbr->filename, _cbr->tablename); 
    62139} 
    63140 
     
    81158int cbr_add_row(cbr _cbr, char *_cols[], float *_vals, unsigned int _len) 
    82159{ 
     160    unsigned int i; 
     161     
     162    // generate command 
     163    printf("%s\n", _cbr->command); 
     164    strcpy(_cbr->command, "insert into "); 
     165    strcat(_cbr->command, _cbr->tablename); 
     166     
     167    strcat(_cbr->command, " ("); 
     168    for (i=0; i<_len; i++) { 
     169        strcat(_cbr->command, _cols[i]); 
     170        if (i != _cbr->num_columns-1) // not last entry 
     171            strcat(_cbr->command, ", "); 
     172    } 
     173    strcat(_cbr->command, ") "); 
     174 
     175    strcat(_cbr->command, " values("); 
     176    for (i=0; i<_len; i++) { 
     177        // ???? how to fill the values if _cbr->num_columns != _len 
     178        // assume = in the following 
     179        sprintf(_cbr->command, "%s%f", _cbr->command, _vals[i]); 
     180        if (i != _cbr->num_columns-1) // not last entry 
     181            strcat(_cbr->command, ", "); 
     182    } 
     183    strcat(_cbr->command, ");"); 
     184     
     185    // execute add command 
     186    ExecuteCommand(_cbr); 
     187 
    83188    return 0; 
    84189} 
  • vtcross/trunk/src/main_cbr.c

    r16 r18  
    2020    int rc; 
    2121 
     22    // create cbr database/table 
    2223    cbr mycbr = cbr_create("ex1", "data", cols, num_cols); 
    2324