Write a library from sqlite3 to Lua

Source: Internet
Author: User

The last time I wrote a mark for the DLL called by Lua, I wrote a small program to try it out. This is because a task is to take data from the sqlite3 database and parse the data into groups. At that time, the simplest idea was to operate the sqlite3 database directly in Lua. Write a small DLL and encapsulate the three simplest functions to Lua: opendb and closedb ha exec. Well, this is even a small supplement written last time. I also encountered the problem of returning the data of the entire table to Lua, but I didn't make it out. I am also familiar with Lua, And I am interested in studying it myself.

 

# 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_st Ate * 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, "the database object is empty and cannot be closed! "); <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/>} 

 

There is nothing to talk about in the program, and a problem occurs on the road. The "tables" table is returned. To put it bluntly, it is to insert a table into the table. This is very simple in the Lua script, but it is a bit dizzy when running to C. However, Lua still provides corresponding operations, but does not carefully look at the search. Let's take a look at how C inserts a table into Lua.

Int luacf_gettable (lua_state * l) <br/>{< br/> lua_newtable (l); // create a table <br/> int ntop = lua_gettop (L ); // obtain the position of the table in the stack (not a simple stack top) <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); // set key <br/> lua_pushstring (L, STR); // set value <br/> lua_settable (L, ntop ); // set key-value pairs to the corresponding table <br/>}< br/> return 1; <br/>} 

This is a string array table with 10 records. But the inserted value is set through lua_pushxxx, but the lua_pushtable function is not provided. What should I do? Lua also provides a lua_insert function, which can place the values in the stack at your specified position. Another function is even more powerful. lua_rawseti, as I wrote in the code, indicates that this function is highly efficient. It seems that when inserting a two-dimensional table, not only the internal efficiency is high, but the code lines are much reduced.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.