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

本文介绍如何使用Lua脚本语言与SQLite3数据库进行交互,包括数据库创建、表操作及数据增删查改等基本功能实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先需要
这里写图片描述
这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> 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值