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菜单时候回调的函数。
原文:http://blog.csdn.net/tonny_guan/article/details/40455429