首先 开始 游戏开始都会有一个 注册功能
于是 构建一个单利的 存储类
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 炒作简单
先做一下简单的介绍
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); }//|
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()); } } } } |
本文介绍了游戏开发中用户数据的存储方法,对比了SQLite和JSON的优缺点,并详细展示了如何利用C++与rapidjson库进行JSON数据的生成与解析。
873

被折叠的 条评论
为什么被折叠?



