lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I write a small program to call a lua file, and in the lua file just call lua-Sqlite3 module. And the program crashed.
I don't know if it relates to lua-Sqlite3? Any advice is kind. Thanks.

The c++ code is test.cpp:
extern "C" {
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
}

#include <iostream>
#include <string>
#include <string.h>
#include <fstream>
#include <sstream>
#include <stdexcept>
#include <memory>
#include <signal.h>
#include <unistd.h>
#include <stdexcept>
#include <sys/types.h>
using namespace std;


int main(int argc, char* argv[])
{
    try
    {
        lua_State* L = lua_open();
        if(NULL == L)
        {
            return -1;
        }
        luaL_openlibs(L);
        if( luaL_dofile(L, "testsqlite.lua") )
        {
            cout<< "do file error" <<endl;
        }
        lua_getglobal(L, "main");
        if(lua_pcall(L, 0, 0, 0)!= 0)
        {
            cout<< "call main error" <<endl;
        }

        lua_close(L);
        cout<< "all work done." <<endl;
    }
    catch(exception& ex)
    {
        cout<< "main_exception_caught[" << ex.what() <<"]" <<endl;
    }
    catch(...)
    {
        cout<< "main_unknown_exception_caught" <<endl;
    }
    return 0;
}

And the testsqlite.lua is:

require "sqlite3"
function recordtx(sname, saddress, sfather)
   local DBH = sqlite3.open("db.sqlite3."..os.date("%Y%m%d"));
   DBH:set_busy_timeout(10 * 1000);

   DBH:exec[[ create table if not exists  txflow
             (
                 name      char(20) not null,
                 address   char(20) not null,
                 father    char(20) not null
             )
          ]];
    local stmt = DBH:prepare[[
               insert into txflow values (:name, :address, :father)
           ]];
    local ret, errmsg = stmt:bind {
        name     = sname,
        address  = saddress,
        father   = sfather
    }:exec()

    DBH:close();
end

function main()
-- for i=1, 36 do --critical value, When I change it to 36, it will crash the program, but when I change it to 35, it worked.
    for i=1, 35 do
        recordtx("david", "chicago", "albert")
    end
end