NoteDAO.cpp中的NoteDAO::create插入备忘录的代码如下: int NoteDAO::create(string pDate, string pContent) { //初始化数据库 initDB(); sqlite3* db= NULL; string path = dbDirectoryFile(); if (sqlite3_open(path.c_str(), &db) != SQLITE_OK) { sqlite3_close(db); CCASSERT(false, "DB open failure."); } else { string sqlStr = "INSERT OR REPLACE INTO note (cdate, content) VALUES (?,?)"; ① sqlite3_stmt *statement; //预处理过程 if (sqlite3_prepare_v2(db, sqlStr.c_str(), -1, &statement, NULL) == SQLITE_OK) { //绑定参数开始 sqlite3_bind_text(statement, 1, pDate.c_str(), -1, NULL); ② sqlite3_bind_text(statement, 2, pContent.c_str(), -1, NULL); //执行插入 if (sqlite3_step(statement) != SQLITE_DONE) { ③ CCASSERT(false, "Insert Data failure."); } } sqlite3_finalize(statement); sqlite3_close(db); } return 0; }
void HelloWorld::OnClickMenu2(cocos2d::Ref* pSender) { string currentTime = MyUtility::getCurrentTime(); log("%s",currentTime.c_str()); NoteDAO::create(currentTime, "欢迎使用MyNote."); }
nt NoteDAO::remove(string pDate) { //初始化数据库 initDB(); sqlite3* db= NULL; string path = dbDirectoryFile(); if (sqlite3_open(path.c_str(), &db) != SQLITE_OK) { sqlite3_close(db); CCASSERT(false, "DB open failure."); } else { string sqlStr = "DELETE from note where cdate =?"; sqlite3_stmt *statement; //预处理过程 if (sqlite3_prepare_v2(db, sqlStr.c_str(), -1, &statement, NULL) == SQLITE_OK) { //绑定参数开始 sqlite3_bind_text(statement, 1, pDate.c_str(), -1, NULL); //执行删除 if (sqlite3_step(statement) != SQLITE_DONE) { CCASSERT(false, "Delete Data failure."); } } sqlite3_finalize(statement); sqlite3_close(db); } return 0; }
void HelloWorld::OnClickMenu3(cocos2d::Ref* pSender) { NoteDAO::remove("2008-08-16 10:01:02"); }
int NoteDAO::modify(string pDate, string pContent) { //初始化数据库 initDB(); sqlite3* db= NULL; string path = dbDirectoryFile(); if (sqlite3_open(path.c_str(), &db) != SQLITE_OK) { sqlite3_close(db); CCASSERT(false, "DB open failure."); } else { string sqlStr = "UPDATE note set content=? where cdate =?"; sqlite3_stmt *statement; //预处理过程 if (sqlite3_prepare_v2(db, sqlStr.c_str(), -1, &statement, NULL) == SQLITE_OK) { //绑定参数开始 sqlite3_bind_text(statement, 1, pContent.c_str(), -1, NULL); sqlite3_bind_text(statement, 2, pDate.c_str(), -1, NULL); //执行修改数据 if (sqlite3_step(statement) != SQLITE_DONE) { CCASSERT(false, "Upate Data failure."); } } sqlite3_finalize(statement); sqlite3_close(db); } return 0; }
void HelloWorld::OnClickMenu4(cocos2d::Ref* pSender) { NoteDAO::modify("2008-08-16 10:01:02", "修改数据。"); }
HelloWorld::OnClickMenu4函数是玩家点击Update Data菜单时候回调的函数。
《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