[16] | 1 | // |
---|
| 2 | // Case-based reasoner |
---|
| 3 | // |
---|
| 4 | |
---|
| 5 | #include <stdlib.h> |
---|
| 6 | #include <string.h> |
---|
| 7 | #include <stdio.h> |
---|
| 8 | |
---|
| 9 | #include "cbr.h" |
---|
| 10 | |
---|
| 11 | struct cbr_s { |
---|
| 12 | char filename[64]; |
---|
| 13 | char tablename[64]; |
---|
| 14 | char command[2048]; |
---|
| 15 | sqlite3 *db; |
---|
| 16 | unsigned int num_columns; |
---|
| 17 | }; |
---|
| 18 | |
---|
| 19 | |
---|
[18] | 20 | // open database or create database is it does not exist |
---|
| 21 | int 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) |
---|
| 42 | int 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 | |
---|
| 52 | int 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 | |
---|
| 67 | |
---|
[16] | 68 | cbr cbr_create(char * _filename, char * _tablename, char * _cols[], unsigned int _len) |
---|
| 69 | { |
---|
[18] | 70 | // cbr is a pointer to struct cbr_s |
---|
[16] | 71 | cbr _cbr = (cbr) malloc(sizeof(struct cbr_s)); |
---|
| 72 | |
---|
[18] | 73 | // create database |
---|
| 74 | |
---|
| 75 | // copy filename |
---|
| 76 | unsigned int i=0; |
---|
[17] | 77 | strcpy(_cbr->filename, _filename); |
---|
[18] | 78 | |
---|
| 79 | // execute create database command |
---|
| 80 | // database handle |
---|
| 81 | //_cbr->db = NULL; |
---|
| 82 | OpenDatabase(_cbr); |
---|
| 83 | |
---|
| 84 | // create table |
---|
| 85 | |
---|
| 86 | // copy tablename |
---|
[17] | 87 | strcpy(_cbr->tablename, _tablename); |
---|
[16] | 88 | |
---|
[18] | 89 | // number of columns in the table |
---|
[16] | 90 | _cbr->num_columns = _len; |
---|
| 91 | |
---|
[18] | 92 | // generate command |
---|
[16] | 93 | strcpy(_cbr->command, "CREATE TABLE "); |
---|
[18] | 94 | strcat(_cbr->command, _cbr->tablename); |
---|
[16] | 95 | strcat(_cbr->command, "("); |
---|
[18] | 96 | for (i=0; i<_cbr->num_columns; i++) { |
---|
[16] | 97 | strcat(_cbr->command, _cols[i]); |
---|
| 98 | strcat(_cbr->command, " FLOAT"); |
---|
[18] | 99 | if (i != _cbr->num_columns-1) // not last entry |
---|
[16] | 100 | strcat(_cbr->command, ", "); |
---|
| 101 | } |
---|
| 102 | strcat(_cbr->command, ");"); |
---|
| 103 | |
---|
[18] | 104 | // execute create table command |
---|
| 105 | ExecuteCommand(_cbr); |
---|
[16] | 106 | |
---|
| 107 | return _cbr; |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | void cbr_free(cbr _cbr) |
---|
| 111 | { |
---|
| 112 | // delete table here |
---|
[18] | 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);*/ |
---|
[16] | 118 | |
---|
[18] | 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); |
---|
| 125 | |
---|
[16] | 126 | free(_cbr); |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | void cbr_print(cbr _cbr) |
---|
| 130 | { |
---|
[18] | 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); |
---|
[16] | 139 | } |
---|
| 140 | |
---|
| 141 | //static |
---|
| 142 | int cbr_callback(void *notUsed, int argc, char **argv, char **azColName) |
---|
| 143 | { |
---|
| 144 | return 0; |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | int cbr_search(cbr _cbr, char *_names[], int * _ops, float *_vals, unsigned int _n, float *_retvals) |
---|
| 148 | { |
---|
| 149 | // set up statement |
---|
| 150 | |
---|
| 151 | char* zErrMsg = NULL; |
---|
| 152 | //sqlite3_exec(_cbr->db, argv[2], callback, NULL, &zErrMsg); |
---|
| 153 | sqlite3_exec(_cbr->db, _cbr->command, cbr_callback, NULL, &zErrMsg); |
---|
| 154 | |
---|
| 155 | return 0; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | int cbr_add_row(cbr _cbr, char *_cols[], float *_vals, unsigned int _len) |
---|
| 159 | { |
---|
[18] | 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 | |
---|
[16] | 188 | return 0; |
---|
| 189 | } |
---|
| 190 | |
---|