Firstly, I have the following code:
map<char*, char*> records;
// id, value are buffer variables to store the text that returns from `strcpy()`
char *id = (char*)malloc(sizeof(char*) * 20);
char *value = (char*)malloc(sizeof(char*) * 20);
sql->open(dbFile);
sql->query("SELECT * FROM categories");
while(sql->fetch() != SQLITE_DONE){
// copy the text that returns from `sql->getValue`, then convert that text from `const unsigned char*` to `const char*`
strcpy(id, reinterpret_cast<const char*>(sql->getValue(0)));
strcpy(value, reinterpret_cast<const char*>(sql->getValue(1)));
// insert the values that in `id`,`value` as new item
records.insert(pair<char*, char*>(id, value));
}
free(id);
free(value);
The idea of the previous code is copy the text that returns this method sql->getValue(0)
by using strcopy()
, then convert that text from const unsigned char*
to const char*
, then store these text in the id
, value
variables, then insert id
, value
as a new item.
The previous steps occur on each data that gets from database.
Clarify the idea more:
For example, There are three rows fetched from database (fruits
, vegetables
, sweets
).
Naturally, when insert these three data into the map container, the number of items that in the container will be three items first(fruits
), second(vegetables
), third(sweets
).
in my case, when checking the number of items, I find there is one item only, and the item is sweets
.
Now, I amazed, where is the rest of items ?