在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
- 在C/C++项目中接入LuaSQLite3
- 在Unity项目中接入ShareSDK
- 在Eclipse中开发C/C++项目
- 在Eclipse中开发C/C++项目
- 在Eclipse中开发C/C++项目
- 在swift 项目中使用objective-c
- 在C/C++项目中集成Lua
- 在Eclipse中开发C/C++项目
- 教您在项目中快速接入第三方应用
- 在as3项目中接入百度翻译Api
- C/S架构中接入层的安全体系
- 在Eclipse中开发C/C++项目-转帖
- 如何在Android系统源码中添加一个C项目?
- C#.在vs2010中打开vs2012的项目
- c语言 如何在项目中使用数组
- 在同一个项目中使用Swift 和 Objective-C
- 如何在IOS项目中调用C的函数
- 在安卓项目中单独编译某个c源文件
- Opencv函数setMouseCallback鼠标事件响应
- NYOJ-88汉诺塔(一)hannuota
- Codeforces Round #293 (Div. 2) C.Anya and Smartphone(模拟)
- CSS设置英文字母大小写
- 首次写blog
- 在C/C++项目中接入LuaSQLite3
- JQuery之复选框checkbox基本操作
- JS循环等待检测
- Git自学之路(三)- Git打标签
- Android Volley框架的几种post提交请求方式
- **MYSQL** 系列一
- 大数据基础(五)从零开始安装配置Hadoop 2.7.2+Spark 2.0.0到Ubuntu 16.04
- Uva 1225 - Digit Counting
- 组合数