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数据。
《Cocos2d-x实战 C++卷》现已上线,各大商店均已开售:
京东:http://item.jd.com/11584534.html
亚马逊:http://www.amazon.cn/Cocos2d-x%E5%AE%9E%E6%88%98-C-%E5%8D%B7-%E5%85%B3%E4%B8%9C%E5%8D%87/dp/B00PTYWTLU
当当:http://product.dangdang.com/23606265.html
互动出版网:http://product.china-pub.com/3770734
《Cocos2d-x实战 C++卷》源码及样章下载地址:
源码下载地址:http://51work6.com/forum.php?mod=viewthread&tid=1155&extra=page%3D1
样章下载地址:http://51work6.com/forum.php?mod=viewthread&tid=1157&extra=page%3D1