I'm new to LUA and luasql and I'm having problems with my C++ code in Windows
(VS2008). I have lua working great in my application and wanted to use sqlite
in my lua scripts.
Here is my test script:
require "luasql.sqlite3"
env = luasql.sqlite3()
conn = env:connect("testsqlite.db3")
assert(conn:execute("create table if not exists tbl1(one, two)"))
assert(conn:execute("insert into tbl1 values('hello!',10)"))
assert(conn:execute("insert into tbl1 values('goodbye',20)"))
cursor = assert(conn:execute("select * from tbl1"))
row = {}
while cursor:fetch(row) do
print(table.concat(row, '|'))
end
cursor:close()
conn:close()
env:close()
This script works using the command line:
PS C:\rev\src\luasqltest> & 'C:\Program Files\Lua\5.1\lua.exe' -e
"io.stdout:setvbuf 'no'" .\sqlite.lua
hello!|10
goodbye|20
However, my application is throwing an unhandled exception (access violation)
when I attempt to run the script above. If I comment out the middle section
from cursor=execute() to cursor:close(), it works just fine. Even
execute(select) actually completes if I comment out the row iteration, but I
get the unhandled exception at lua_close(L). If I run the full script, my
application crashes at lua_pcall(L,0,0,0).
I have lua5.1.dll and luasql/sqlite3.dll accessible to the project; I got them
from the lua for windows 5.1.4-40 install. Here is my reproduction:
/// @file main.cpp
/// @author rev
#include <string>
#include <iostream>
#include <stdexcept>
extern "C"
{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
int panic( lua_State* L )
{
std::string err( lua_tostring( L, -1 ) );
throw std::runtime_error( err.c_str() );
return 0;
}
int main( int argc, char* argv[] )
{
lua_State* L = lua_open();
luaL_openlibs( L );
lua_atpanic( L, panic );
try
{
int n = luaL_loadfile( L, "sqlite.lua" );
switch ( n )
{
case 0 :
{
switch ( lua_pcall( L, 0, 0, 0 ) )
{
case 0 :
// successfully ran the script
break;
case LUA_ERRRUN :
case LUA_ERRMEM :
case LUA_ERRERR :
lua_error( L );
default :
throw std::runtime_error( "lua_pcall -
unknown error" );
}
}
break;
case LUA_ERRFILE :
case LUA_ERRSYNTAX :
case LUA_ERRMEM :
lua_error( L );
default :
throw std::runtime_error( "luaL_loadfile - unknown
error" );
}
}
catch ( const std::exception& e )
{
std::cerr << e.what() << std::endl;
}
try
{
lua_close( L );
}
catch ( const std::exception& e )
{
std::cerr << e.what() << std::endl;
}
return 0;
}
I must be missing something basic. If you want a zip of my project, I can
provide it.
RW
Ron Wilson, Engineering Project Lead
(o) 434.455.6453, (m) 434.851.1612, www.harris.com
_______________________________________________
Kepler-Project mailing list
[email protected]
http://lists.luaforge.net/cgi-bin/mailman/listinfo/kepler-project
http://www.keplerproject.org/