zoukankan      html  css  js  c++  java
  • Cocos2d-x数据持久-变更数据

    当数据变化,参与SQL报表insert、update和delete声明。这项3个月SQL语句可以带参数。

    详细过程的数据,例如,下面的变化看出。
    (1) 采用sqlite3_open开放式数据库功能。
    (2) 使用sqlite3_prepare_v2函数预处理SQL语句。
    (3) 使用sqlite3_bind_text函数绑定參数。
    (4) 使用sqlite3_step函数运行SQL语句。
    (5) 使用sqlite3_finalize和sqlite3_close函数释放资源。
    这与查询数据少了提取字段数据这个步骤,其它步骤是一样的。以下我们看看代码部分。


    1、插入备忘录

    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; }



    上述代码第①行是插入数据的SQL语句,当中的问号(它是占位符)就是要绑定的參数。第②行代码sqlite3_bind_text(statement, 1, pDate.c_str(), -1, NULL)是绑定第一个參数。
    第③行代码中的sqlite3_step(statement)语句运行插入语句,常量SQLITE_DONE表示运行完毕。
    为了可以调用NoteDAO中插入备忘录函数create。我们须要在HelloWorldScene场景中调用。HelloWorldScene.cpp主要代码例如以下:
    void HelloWorld::OnClickMenu2(cocos2d::Ref* pSender)
    {
    	string currentTime = MyUtility::getCurrentTime();
    	log("%s",currentTime.c_str());
    	NoteDAO::create(currentTime, "欢迎使用MyNote.");
    }


    HelloWorld::OnClickMenu2函数是玩家点击Insert Data菜单时候回调的函数。当中MyUtility::getCurrentTime()语句是获得当前时间。


    提示  在Win32平台,採用Visual Studio 进行编译执行时候中文会有一些麻烦!我们须要将源码文件另存为Unicode(UTF-8无签名)格式。能够參考4.2.5一节解决方法,有的时候会有例如以下编译错误:error C2001: 常量中有换行符。我们须要在中文字符后面加入一些英文字符。或者“啊”等特殊的中文字符。

    比如:"欢迎使用MyNote。"这个字符串后面是中文句号“。”结尾,编译的时候就会出现error C2001错误。本例中我们採用英文句号“.”结尾,编译就不会有错误,可是会有警告。

    这样的问题仅仅会出如今Win32平台,其他平台没有问题。为了省事我们測试时候能够不採用中文字符。




    2、删除备忘录
    NoteDAO.cpp中的NoteDAO::remove删除备忘录的代码例如以下:
    i

    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;
    }
    


    为了可以调用NoteDAO中删除备忘录函数remove。我们须要在HelloWorldScene场景中调用。

    HelloWorldScene.cpp主要代码例如以下:

    void HelloWorld::OnClickMenu3(cocos2d::Ref* pSender)
    {
    	NoteDAO::remove("2008-08-16 10:01:02");
    }


    HelloWorld::OnClickMenu3函数是玩家点击Delete Data菜单时候回调的函数。


    3、改动备忘录
    NoteDAO.cpp中的NoteDAO::modify改动备忘录的代码例如以下:
    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; }



    为了可以调用NoteDAO中改动备忘录函数modify。我们须要在HelloWorldScene场景中调用。HelloWorldScene.cpp主要代码例如以下:
    void HelloWorld::OnClickMenu4(cocos2d::Ref* pSender)
    {
    	NoteDAO::modify("2008-08-16 10:01:02", "改动数据。

    "); }



    HelloWorld::OnClickMenu4函数是玩家点击Update Data菜单时候回调的函数。



    很多其它内容请关注最新Cocos图书《Cocos2d-x实战 C++卷》
    本书交流讨论站点:http://www.cocoagame.net
    很多其它精彩视频课程请关注智捷课堂Cocos课程:http://v.51work6.com
    欢迎增加Cocos2d-x技术讨论群:257760386


    《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

    欢迎关注智捷iOS课堂微信公共平台


    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    hadoop-2.7.3安装kafka_2.11-2.1.0
    HBase数据快速导入之ImportTsv&Bulkload
    hbase shell 基本操作
    SQLplus命令中删除键和翻页键不能用的问题
    SQL*Loader 的使用sqlldr和sqluldr2方法详解
    python连接oracle导出数据文件
    python零碎知识点一
    用Python输出一个Fibonacci数列
    问题总结——window平台下gruntower安装后无法运行的问题
    JavaScript学习笔记——浅拷贝、深拷贝
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/4823237.html
Copyright © 2011-2022 走看看