在C/C++项目中接入LuaSQLite3
2016-07-25 21:43
363 查看
首先需要
这3个文件,lsqlite3.c下载地址:
这里写链接内容
sqlite3.c和sqlite3.h下载地址:
这里写链接内容
将这3个文件导入到项目中,并增加一个头文件lsqlite3.h,
将sqlite3注册到Lua中:
在DataBase.lua中:
结果
验证是否能创建数据库:
运行之后在指定目录下创建了config
完善对config数据库的操作
运行结果
通过终端验证
这3个文件,lsqlite3.c下载地址:
这里写链接内容
sqlite3.c和sqlite3.h下载地址:
这里写链接内容
将这3个文件导入到项目中,并增加一个头文件lsqlite3.h,
// // lsqlite3.h // LuaAndCpp // #ifndef lsqlite3_h #define lsqlite3_h int luaopen_lsqlite3(lua_State* L); #endif /* lsqlite3_h */
将sqlite3注册到Lua中:
// // main.cpp // LuaAndCpp // #include <iostream> #include "lua.hpp" #if __cplusplus extern "C"{ #endif #include "lsqlite3.h" static luaL_Reg lua_sqlite3Lib[] = { {"lsqlite3",luaopen_lsqlite3}, {NULL,NULL} }; #if __cplusplus } #endif int 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) end local 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 tab end -- 删除 sq.delete = function ( table_name ) local str = 'delete from '..table_name db:exec(str) end local 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 scale 720 height 1280 width 1 rotation 2 height 3 width Program ended with exit code: 0
通过终端验证
ForestdeMBP:LuaAndCpp Forest$ sqlite3 config SQLite version 3.8.10.2 2015-05-20 18:17:19 Enter ".help" for usage hints. sqlite> .tables sceneInfo sqlite> .schema CREATE TABLE sceneInfo (id integer primary key , content); sqlite> select * from sceneInfo; 1|scale 720|height 1280|width sqlite> .mode line sqlite> select * from sceneInfo; id = 1 content = scale id = 720 content = height id = 1280 content = width sqlite> .mode column sqlite> select * from sceneInfo; 1 scale 720 height 1280 width sqlite> .headers on sqlite> select * from sceneInfo; id content ---------- ---------- 1 scale 720 height 1280 width sqlite>
相关文章推荐
- Lua版本冒泡算法及其优化
- lua math库
- lua string库
- Lua闭包的详解
- Lua 读表代码,自动生成字段名字
- lua 语法
- c++使用luabind示例
- [LintCode] Evaluate Reverse Polish Notation 计算逆波兰表达式
- LUA 函数式编程demo
- lua 字符串处理
- Lua Go R HEXO 简单介绍
- LUA 创建文件和文件夹
- lua lfs库
- 150. Evaluate Reverse Polish Notation
- 一、windows 下安装并使用 lua
- 如何创建一个 Lua 模块
- 如何创建一个 Lua 模块
- LUA学习笔记(第18-20章)
- LUA学习笔记(第18-20章)
- 安卓属性动画之插值器(TimeInterpolator)和估值器(TypeEvaluator)