在C/C++项目中接入LuaSQLite3

来源:互联网 发布:c语言fabs什么意思 编辑:程序博客网 时间:2024/06/09 13:40

首先需要
这里写图片描述
这3个文件,lsqlite3.c下载地址:
这里写链接内容
sqlite3.c和sqlite3.h下载地址:
这里写链接内容

将这3个文件导入到项目中,并增加一个头文件lsqlite3.h,
这里写图片描述

////  lsqlite3.h//  LuaAndCpp//#ifndef lsqlite3_h#define lsqlite3_hint luaopen_lsqlite3(lua_State* L);#endif /* lsqlite3_h */

将sqlite3注册到Lua中:

////  main.cpp//  LuaAndCpp//#include <iostream>#include "lua.hpp"#if __cplusplusextern "C"{#endif#include "lsqlite3.h"    static luaL_Reg lua_sqlite3Lib[] = {        {"lsqlite3",luaopen_lsqlite3},        {NULL,NULL}    };#if __cplusplus}#endifint main(int argc, const char * argv[]) {    // insert code here...    std::cout << "Hello, World!\n";    lua_State* m_luaState = luaL_newstate();    luaL_openlibs(m_luaState);    luaL_Reg* lib = lua_sqlite3Lib;    lua_getglobal(m_luaState, "package");    lua_getfield(m_luaState, -1, "preload");    for (; lib->func; lib++) {        lua_pushcfunction(m_luaState, lib->func);        lua_setfield(m_luaState, -2, lib->name);    }    lua_pop(m_luaState, 2);    luaL_dofile(m_luaState, "/Users/Forest/Documents/LuaAndCpp/LuaAndCpp/scripts/DataBase.lua");    lua_close(m_luaState);    return 0;}

在DataBase.lua中:

local sqlite3 = require "lsqlite3"print('--->>',sqlite3)

结果--->> table: 0x100400cf0

验证是否能创建数据库:

local sq = {}local sqlite3 = require "lsqlite3"local db = nil -- print('--->>',sqlite3)sq.create_db = function ( path , name )    path = path..name    db = sqlite3.open(path)endlocal path = "/Users/Forest/Documents/LuaAndCpp/LuaAndCpp/"sq.create_db(path,"config")return sq

运行之后在指定目录下创建了config

这里写图片描述

完善对config数据库的操作

local sq = {}local sqlite3 = require "lsqlite3"local db = nil -- 创建数据库sq.create_db = function ( path , name )    path = path..name    db = sqlite3.open(path)end-- 创建表sq.create_table = function ( table_name )    local str = 'create table '..table_name..' (id integer primary key , content);'     db:exec(str)end-- 增sq.insert_info = function ( table_name , key , value )    local str = 'insert into '..table_name..' values('..key..','..'\"'..value..'\"'..');'    print(str)    db:exec(str)end-- 查找sq.select = function ( table_name )    local str = 'select * from '..table_name    local tab = {}    for row in db:nrows(str) do         print(row.id,row.content)        -- tab[row.id] = row.content        if row.id == 1 then            row.content = "rotation"        end        local index = tonumber(row.id)        local content = tostring(row.content)        table.insert(tab , content)    end    for k,v in pairs(tab) do        print(k,v)    end    return tabend-- 删除sq.delete = function ( table_name )    local str = 'delete from '..table_name    db:exec(str)endlocal path = "/Users/Forest/Documents/LuaAndCpp/LuaAndCpp/"sq.create_db(path,"config")-- 创建 sceneInfo 表sq.create_table('sceneInfo')sq.insert_info('sceneInfo', 1280, "width")sq.insert_info('sceneInfo', 720,  "height")sq.insert_info('sceneInfo', 1, "scale")sq.select('sceneInfo')return sq

运行结果

Hello, World!insert into sceneInfo values(1280,"width");insert into sceneInfo values(720,"height");insert into sceneInfo values(1,"scale");1   scale720 height1280    width1   rotation2   height3   widthProgram ended with exit code: 0

通过终端验证

ForestdeMBP:LuaAndCpp Forest$ sqlite3 config SQLite version 3.8.10.2 2015-05-20 18:17:19Enter ".help" for usage hints.sqlite> .tablessceneInfosqlite> .schemaCREATE TABLE sceneInfo (id integer primary key , content);sqlite> select * from sceneInfo;1|scale720|height1280|widthsqlite> .mode linesqlite> select * from sceneInfo;     id = 1content = scale     id = 720content = height     id = 1280content = widthsqlite> .mode columnsqlite> select * from sceneInfo;1           scale     720         height    1280        width     sqlite> .headers onsqlite> select * from sceneInfo;id          content   ----------  ----------1           scale     720         height    1280        width     sqlite> 
0 0