今天 无聊写写游戏玩玩 实战sqlite3

 首先 开始 游戏开始都会有一个 注册功能

于是 构建一个单利的 存储类

create table UserTable(UserName text,Password text,constraint PK_U primary key(UserName))

这个句的意思是 构建一个用户表 其内容为 UserName   Password    

constraint PK_U primary key   这句话的意思是键值唯一性

用户名不可重复

 随后在构建了一个GmaeDtateTbale 表 来存储 一切数据

create table GameDataTable(UserName text,Propety text,Value integer,constraint PK_UP primary key(UserName,Propety))

constraint PK_UP primary key(UserName,Propety)

俩则不可一样

库创建步骤

if (CCFileUtils::sharedFileUtils()->isFileExist(dbPath))

判断是否存在

如果存在

sqlite3 *mydb;
char *error;
int result;
result = sqlite3_open(dbPath.c_str(), &mydb);

随后就是 顺水推舟 一切都轻松

但是 发现 效率上没有json 来的快  

最好还是改json

json 炒作简单

先做一下简单的介绍

json 语法规则:

    (1)数据在“名称/值对”中,即 键值对(key-value)形式。

    (2)每条数据由“逗号”分隔。

    (3)“花括号”{ } 保存 对象

    (4)“方括号”[ ] 保存 数组

json 操作:

  rapidjson::Document d;
 
//[2] 获取分配器
     rapidjson::Document::AllocatorType& allocator = d.GetAllocator();
 
//[3] 设置为对象格式 SetObject
     d.SetObject();
 
//[4] 添加数据
     //[4.1] 往json对象中添加数据:名称/值对
     rapidjson::Value object(rapidjson::kObjectType);  // 创建对象
 
     object.AddMember( "int" , 1, allocator);          // 添加 "int" : 1
     object.AddMember( "double" , 1.1, allocator);     // 添加 "double" : 1.1
     object.AddMember( "hello" "world" , allocator);  // 添加 "hello" : "world"
 
     //[4.2] 往json数组中添加数据:值
     rapidjson::Value array(rapidjson::kArrayType);  // 创建数组
 
     rapidjson::Value str(rapidjson::kStringType);   // 字符串
     rapidjson::Value obj(rapidjson::kObjectType);   // 对象
     str.SetString( "hello" );  // 设置str的值
     obj.AddMember( "name" "alice" , allocator);
     obj.AddMember( "age" , 23, allocator);
 
     array.PushBack(123, allocator);    // 添加数字
     array.PushBack( "888" , allocator);  // 添加字符串,方式一
     array.PushBack(str, allocator);    // 添加字符串,方式二
     array.PushBack(obj, allocator);    // 添加对象
 
     //[4.3] 往对象格式的json文件中添加数据
     d.AddMember( "hello" "world" , allocator);
     d.AddMember( "object" , object, allocator);
     d.AddMember( "array" , array, allocator);
 
//[5] 将json数据写入文件中
     StringBuffer buffer;
     rapidjson::Writer<StringBuffer> writer(buffer);
     d.Accept(writer);
     CCLOG( "%s" , buffer.GetString());
 
     FILE * file =  fopen ( "/soft/cocos2d-x-3.4/projects/Demo34/Resources/testJson.json" "wb" );
     if (file) {
         fputs (buffer.GetString(), file);
         fclose (file);
     }
//

Json解析使用举例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//
//[1] 读取json文件内容
     std::string str = FileUtils::getInstance()->getStringFromFile( "testJson.json" );
     CCLOG( "%s" , str.c_str());
 
//[2] 创建用于处理json代码的类
     // 创建rapidjson::Document类:用于操作json代码
     rapidjson::Document d;
 
//[3] 解析json文件内容
     // 其中 rapidjson::kParseDefaultFlags = 0,默认方式
     d.Parse<rapidjson::kParseDefaultFlags>(str.c_str());
     // d.Parse<0>(str.c_str());  // 也可以直接写<0>
 
//[4] 判断解析是否出错
     if  (d.HasParseError()) {
         CCLOG( "GetParseError %s\n" ,d.GetParseError());
         return ;
     }
 
//[5] 获取json中的数据
     // 判断json文件是否为数组格式
     if  (d.IsArray()) {
         
         rapidjson::Value& array = d;
         
         for  ( int  i = 0; i < array.Size(); i++) {
 
             if  (d[i].IsBool()) {    // 逻辑值
                 CCLOG( "%d is Bool : %d" , i, array[i].GetBool());
             }
             if  (d[i].IsNumber()) {  // 数字
                 CCLOG( "%d is Number : %d" , i, array[i].GetInt());
             }
             if  (d[i].IsString()) {  // 字符串
                 CCLOG( "%d is String : %s" , i, array[i].GetString());
             }
             if  (d[i].IsObject()) {  // 对象
                 rapidjson::Value& object = d[i];
                 CCLOG( "%d is Object : %s" , i, array[i][ "name" ].GetString());
                 CCLOG( "%d is Object : %d" , i, object[ "age" ].GetInt());
             }
             if  (d[i].IsArray()) {   // 数组
                 for  ( int  j = 0; j < array[i].Size(); j++) {
                     CCLOG( "[%d,%d] is Array : %d" , i, j, array[i][j].GetInt());
                 }
             }
         }
     }

看完介绍 是不是 简单 的一塌糊涂

写个单机 或是 rpg 游戏 就是很轻松了



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值