19 | | int cbr_callback(void *notUsed, int argc, char **argv, char **azColName); |
| 19 | |
| 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 | |
| 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); |
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); |
| 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 | |