寫個Sqlite3到Lua的library

來源:互聯網
上載者:User

前言:上次自己給編寫Lua調用的dll留了個記號,這次正好寫了個小的程式來試下。因為接到個任務是從sqlite3資料庫裡拿資料並且做解析分組之類的工作。當時自然最簡單的想法就是,直接在Lua中能操作sqlite3資料庫就可以了嘛。就寫了個小的dll,封裝了3個最簡單的函數給Lua:openDB、closeDB哈exec。好吧,這個就算是上次寫的一個小補充吧。中間還有碰到給Lua返回整張表資料的問題,不過我沒有鋪開,我對Lua也是一知半解,有興趣的自己研究下。

 

#include "..//comm.h"<br />#include <string><br />#include <sstream><br />#include "sqlite3.h"<br />static sqlite3 *pDB = NULL;<br />// open database<br />int luacf_openDB(lua_State* L)<br />{<br />std::string dbName = lua_tostring(L, 1);<br />lua_Integer rc = sqlite3_open(dbName.c_str(), &pDB);<br />lua_pushinteger(L, rc);<br />return 1;<br />}<br />// close database<br />int lucacf_closeDB(lua_State* L)<br />{<br />if (pDB)<br />{<br />lua_Integer rc = sqlite3_close(pDB);<br />lua_pushinteger(L, rc);<br />return 1;<br />}<br />lua_pushboolean(L, 0);<br />lua_pushstring(L, "資料庫物件為空白,不能關閉!");<br />return 2;<br />}<br />// exec<br />int luacf_exec(lua_State* L)<br />{<br />bool bRet = false;<br />if (pDB)<br />{<br />sqlite3_stmt* pStmt = NULL;<br />lua_Integer rc = sqlite3_prepare(pDB, lua_tostring(L, 1), lua_strlen(L, 1), &pStmt, NULL);<br />lua_pushinteger(L, rc);<br />while (rc == SQLITE_OK)<br />{<br />int nColumn = sqlite3_column_count(pStmt);<br />if (nColumn <= 0)<br />{<br />sqlite3_finalize(pStmt);<br />bRet = true;<br />lua_pushboolean(L, 1);<br />lua_pushnil(L);<br />break;<br />}<br />// create result_data_table<br />lua_newtable(L);<br />int nTop = lua_gettop(L);<br />int nIndex = 0;<br />while (SQLITE_ROW == sqlite3_step(pStmt))<br />{<br />// create row_data_table<br />lua_newtable(L);<br />int nRowTop = lua_gettop(L);<br />for (int i = 1; i <= nColumn; i ++)<br />{<br />const unsigned char* pszData = sqlite3_column_text(pStmt, i - 1);<br />// lua_pushinteger(L, i);<br /> if (pszData)<br /> lua_pushstring(L, (const char*)pszData);<br /> else<br /> lua_pushstring(L, "");<br />// set data to row_data_table<br />lua_rawseti(L, nRowTop, i);<br />//lua_settable(L, nRowTop);<br />}<br />// set row_data_table to result_data_table<br />lua_rawseti(L, nTop, ++nIndex);<br />// lua_pushinteger(L, nIndex++);<br />// lua_insert(L, nRowTop);<br />// lua_settable(L, nTop);<br />}<br />sqlite3_finalize(pStmt);<br />lua_pushinteger(L, nIndex);<br />break;<br />} // end while (rc == SQLITE_OK)<br />return 3;<br />} // end if (pDB)<br />lua_pushboolean(L, bRet);<br />return 1;<br />}<br />// library<br />luaL_reg libs[] = {<br />{"openDB", luacf_openDB},<br />{"closeDB", lucacf_closeDB},<br />{"exec", luacf_exec},<br />{NULL, NULL}<br />};<br />LUALIB_API int luaopen_luaSqlite3(lua_State* L)<br />{<br />luaL_register(L, "sqlite3lib", libs);<br />return 1;<br />} 

 

程式沒什麼好講的,路上就遇到一個問題,返回二維表。說白了就是往表裡面塞表。在Lua指令碼中這是很簡單的事情,但是跑到C中,就有點暈了。不過Lua還是提供了對應的操作,只是沒仔細看仔細搜。先來看下C往Lua中如何插入表格吧。

int luacf_gettable(lua_State* L)<br />{<br />lua_newtable(L);// 建立table<br />int nTop = lua_gettop(L);// 得到這個table在棧中的位置(不是簡單的棧頂)<br />CString str1 = "str";<br />const char* prefix = "tail ";<br />for(int i = 1; i <= 10; i++, str += prefix)<br />{<br />lua_pushnumber(L, i);// 設定key<br />lua_pushstring(L, str);// 設定value<br />lua_settable(L, nTop);// 將索引值對設定入對應的表中<br />}<br />return 1;<br />} 

這就是一個字串數組table,有10條記錄。但是插入value是通過lua_pushxxx設定的,但是並沒有提供lua_pushtable這樣的函數,那怎麼辦呢?lua還提供了一個lua_insert的函數,呵呵,這個可以將堆棧中的值放到你指定的位置。還有個函數更牛逼了,lua_rawseti,就像我開始代碼中寫的那樣,文檔中說是這個函數效率高。看來在插入二維表的時候,不僅僅是內部效率高,連程式碼都省了好多,呵呵。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.