ValueMap NoteDAO::findById(string pDate) { //初始化数据库 initDB(); sqlite3* db= NULL; ValueMap dict ; string path = dbDirectoryFile(); if (sqlite3_open(path.c_str(), &db) != SQLITE_OK) { ① sqlite3_close(db); CCASSERT(false, "DB open failure."); } else { string qsql = "SELECT cdate,content FROM Note where cdate =?"; sqlite3_stmt *statement; //预处理过程 if (sqlite3_prepare_v2(db, qsql.c_str(), -1, &statement, NULL) == SQLITE_OK) { ② //绑定参数开始 sqlite3_bind_text(statement, 1, pDate.c_str(), -1, NULL); ③ //执行 if (sqlite3_step(statement) == SQLITE_ROW) { ④ char *cdate = (char *)sqlite3_column_text(statement, 0); ⑤ char *content = (char *)sqlite3_column_text(statement, 1); dict["date"] = Value(cdate); dict["content"] = Value(content); } } sqlite3_finalize(statement); ⑥ sqlite3_close(db); ⑦ } return dict; }
{ //初始化数据库 initDB(); sqlite3* db= NULL; string path = dbDirectoryFile(); ValueVector listData; if (sqlite3_open(path.c_str(), &db) != SQLITE_OK) { sqlite3_close(db); CCASSERT(false, "DB open failure."); } else { string qsql = "SELECT cdate,content FROM Note"; sqlite3_stmt *statement; //预处理过程 if (sqlite3_prepare_v2(db, qsql.c_str(), -1, &statement, NULL) == SQLITE_OK) { //执行 while (sqlite3_step(statement) == SQLITE_ROW) { char *cdate = (char *)sqlite3_column_text(statement, 0); char *content = (char *)sqlite3_column_text(statement, 1); ValueMap dict ; dict["date"] = Value(cdate); dict["content"] = Value(content); listData.push_back(Value(dict)); } } sqlite3_finalize(statement); sqlite3_close(db); } return listData; }
while (sqlite3_step(statement) == SQLITE_ROW) { …… }
void HelloWorld::OnClickMenu5(Ref* pSender) { auto arry = NoteDAO::findAll(); ① for (auto& v: arry) { log("-----------------"); ValueMap row = v.asValueMap(); ② string date = row["date"].asString(); ③ string content = row["content"].asString(); ④ log("===>date: : %s", date.c_str()); log("===>content: : %s", content.c_str()); } }
HelloWorld::OnClickMenu5函数是玩家点击Read Data菜单时候回调的函数。其中代码第①行是调用NoteDAO中查询所有函数findAll (),第②行代码是把从列表中取出的元素v,转换为ValueMap字典类型。第③行代码是从字典中取值date数据。第④行代码是从字典中取值content数据。
原文地址:http://blog.csdn.net/tonny_guan/article/details/40431987