user-0 提问: lua请教

回复:
user: user-0
content: lgbxyz 你好，我的lua脚本是 #!/usr/local/bin/lua  function mysub(a,b)         return a+b; end function mytest()         local i=0;         local luasql = require "luasql.mysql"          -- create environment object          env = assert (luasql.mysql())         -- connect to data source         con = assert (env:connect("mylct", "root", "hjzx", "127.0.0.1", 3306))          -- retrieve a cursor         cur = assert (con:execute"SELECT id,pos,offset,type from analog_info")    --获取数据         -- print all rows         row = cur:fetch ({}, "a") -- the rows will be indexed by field names    --显示出来         while row do           -- print(row.type)         print(string.format("%02d id: %d, pos: %d  type:%s",i, row.id, row.pos,row.type))         -- print(i.."  cid:"..)         i=i+1;         row = cur:fetch (row, "a") -- reusing the table of results         end         print "this is the test";         -- close everything         cur:close()         con:close()         env:close() end 我的C程序是： #include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> //lua头文件  #include "/usr/local/include/lua.h" #include "/usr/local/include/lualib.h" #include "/usr/local/include/lauxlib.h"   #define err_exit(num,fmt,args)  \     do{printf("[%s:%d]"fmt"\n",__FILE__,__LINE__,##args);exit(num);} while(0)  #define err_return(num,fmt,args)  \     do{printf("[%s:%d]"fmt"\n",__FILE__,__LINE__,##args);return(num);} while(0)  void stackDump(lua_State* L);   int main(int argc,char**argv) { 	int ret = 0 ; 	char sc[50] = {"test.lua"};      	lua_State * l = luaL_newstate() ;        //创建lua运行环境     	if ( l == NULL ) { 		printf("luaL_newstat() failed\n");      		return 1;      	}      	luaL_openlibs(l);     	ret = luaL_loadfile(l,sc) ;      //加载lua脚本文件     	if ( ret != 0 )  { 		printf("luaL_loadfile failed: %s\n",lua_tostring(l,-1));      		return 1;      	}  	lua_pcall(l,0,0,0); 	lua_getglobal(l,"mytest"); 	if(1 == (ret = lua_isfunction(l,-1))) 	{      		ret = lua_pcall(l,0,0,0) ;     		if ( ret != 0 ) printf("lua_pcall failed[%d]:%s\n",ret,lua_tostring(l,-1)) ; 		else 		{ 			printf("the result is:%d\n",lua_tointeger(l,-1)); 		} 	} 	else{ 		printf("%d ----> 3\n",ret); 	}     	lua_close(l) ;                     //释放lua运行环境     	return 0 ; }

user: user-0
content: sniffer12345 感谢你的回复，我加了local也是同样的错误

user: user-0
content: 你好，感谢你的回复。 我加了local也是同样的错误  引用 1 楼 lgbxyz 的回复:C是怎么调用Lua的?

user: user-1
content: 引用 楼主 sql2zy 的回复:我的lua脚边是（test.lua）：   luasql = require "luasql.mysql"   i =1; env = assert (luasql.mysql()) con = assert (env:connect("mylct", "root", "123456", "127.0.0.1", 3306))  cur = assert (con:execute"SELECT calldate,src,dst from cdr limit 10")    --获取数据  row = cur:fetch ({}, "a")  while row do print(string.format("%02d calldate: %s, callerid: %s  dnid:%s",i, row.calldate, row.src,row.dst)) i=i+1; row = cur:fetch (row, "a") end  cur:close() con:close() env:close()  执行lua test.lua，输出都正常，但是在c程序中调用这个test.lua就会出错，通过gdb查到的信息是： Program terminated with signal 11, Segmentation fault. #0  0x0066a7ab in newkey () from /usr/local/lib/lua/5.1/luasql/mysql.so (gdb) bt #0  0x0066a7ab in newkey () from /usr/local/lib/lua/5.1/luasql/mysql.so #1  0x00661f0d in lua_rawset () from /usr/local/lib/lua/5.1/luasql/mysql.so #2  0x006606e3 in cur_fetch () from /usr/local/lib/lua/5.1/luasql/mysql.so #3  0x0804d034 in luaD_precall () #4  0x08055a31 in luaV_execute () #5  0x0804d4d0 in luaD_call () #6  0x0804a8a1 in f_call () #7  0x0804cbc7 in luaD_rawrunprotected () #8  0x0804cc2f in luaD_pcall () #9  0x0804a6f7 in lua_pcall () #10 0x08049763 in main (argc=2, argv=0xbfe8ba04) at test.c:45 请高手给点提示，这是怎么回事？ 拜托 变量前加local好不好 否则的话是全局变量

user: user-2
content: C是怎么调用Lua的?