Cocos2d-x开发系列 lua sqlite集成 LSQLite3

    项目中经常会使用到sqlite来存储数据,例如聊天,以及一些游戏中的配置。
    Cocos2d-x引擎中是有一个storage文件夹,仔细看LocalStorage.h文件,里面说明了,值专门针对JS Bindings使用。另外没有sqlite.c文件。引擎中也没有SQLite的sqlite.c实现文件。所以我们需要到官网去下载:
luaSqlite的源代码http://lua.sqlite.org/index.cgi/index
下载sqlite的源代码 http://sqlite.org/
引擎版本:3.6

一、集成到项目中
在上面的两个网址下载文件后等到如下文件:
lsqlite3.c (lua的绑定实行已经做好了)
shell.c
sqlite3.c
sqlite3.h
sqlite3ext.h
1、在Class里面新建文件夹Sqlite3,将上述文件放在里面。编译
2、lua-bindings中的auto目录下新建lsqlite3.h文件,内容如下

#ifndef __LSQLITE3_H__
#define __LSQLITE3_H__

#ifdef __cplusplus
extern "C" {
#endif
#include "tolua++.h"
#ifdef __cplusplus
}
#endif

extern "C" int luaopen_lsqlite3(lua_State* L);   //lsqlite3.c 中的C函数,这里注册C函数
#endif

3、注册SQLite相关函数
在L状态中调用lsqlite.h文件中的 luaopen_lsqlite3注册到lua虚拟机环境中。
跟其它的自定义类,或者引擎相关的注册函数调用出调用。这里我们在LuaSupportFactory::registerCustomLuaModule()中添加注册代码

void LuaSupportFactory::registerCustomLuaModule()
{
     //TODO 注册自定义的Lua API
     lua_State* L = LuaEngine::getInstance()->getLuaStack()->getLuaState();

     register_all_ww_net(L);
    register_all_ww_gui(L);
    register_all_ww_data(L);
     luaopen_lsqlite3(L); //注册LSQLite3相关内容。
}

4、测试验证
在lua中调用如下

    local sqlite3 = require("sqlite3")
    cclog("sqlite3  version"..sqlite3.version())

二、关于包体大小
集成的时候,发现,几个文件加起来达到6.78M。主要是sqlite3.c文件很大。不过其实编译后就很小了。
win下的大小
win32下面文件变为2.16M了,但是看看其他的cpp文件编译后大小比这大多了。

新建一个Cocos2d-x lua项目
分别打出不包含、包含lsqlite3的两个项目包
包体大小比较
文件差不多大。所以sqlite.c文件很大,但是因为是C文件,在打包后会压缩到很小。不必担心集成包大小

三、使用API
1、引用LuaSqlite

local sqlite3 = require("sqlite3")

--打开数据库文件
local db = sqlite3.open('test.db')

--打开内存数据库
local db = sqlite3.open_memory()

四、碰到的问题
1、error loading module ‘sqlite3’
在一次更新代码的时候将sqlite注册到Lua的代码冲掉了,一直报这个错误,后来检查代码才发现是没有注册过去。
2、iOS平台下,出现shell.c报错
需要将这个文件去掉,这个文件是控制台数据库操作用的,不应该加搞项目中。

五、实例

-------------------------------------------------------------------------
-- Desc:    sqlite3test.lua
-- Author:  sqlite3测试
-- Copyright (c) wawagame Entertainment All right reserved.
   -- local sqlite3 = require("app.dataorm.sqlite3test")
    -- sqlite3:getDBVersion()
    -- sqlite3:openDB()
    -- sqlite3:insert('numbers', {10086, 10086,"diyal"})
    -- sqlite3:test()
    -- sqlite3:test2()
    -- sqlite3:test3()
    -- sqlite3:aggregate()
    -- sqlite3:crudTest()
    -- sqlite3:statement()
    -- sqlite3:tracing()
    -- sqlite3:batchsql()
    -- sqlite3:updateHook()
-------------------------------------------------------------------------
local sqlite3 = require("sqlite3")

local sqlite3test = class('sqlite3test')

local _db, _vm  --数据库句柄, 数据库状态

--[[获取版本号]]
function sqlite3test:getDBVersion()
    cclog("[SQLite] DB version : "..sqlite3.version())
end

function sqlite3test:openDB()
    local dbFilePath = device.writablePath..'test.db'
    local isExist = cc.FileUtils:getInstance():isFileExist(dbFilePath)

    _db = sqlite3.open(dbFilePath)
    if isExist then
        cclog('[SQLite] DB File is exist')
    else
        cclog('[SQLite] DB File is not exist, created it')
        --初始化表结构
        self:initDB()
    end
end

function sqlite3test:initDB()
    -- Demo表DDL语句
    local t_demo_sql=
    [=[
        CREATE TABLE numbers(num1,num2,str);
        INSERT INTO numbers VALUES(1,11,"ABC");
        INSERT INTO numbers VALUES(2,22,"DEF");
        INSERT INTO numbers VALUES(3,33,"UVW");
        INSERT INTO numbers VALUES(4,44,"XYZ");
        SELECT * FROM numbers;
    ]=]

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值