从github拿下 luasql 源码 https://github.com/keplerproject/luasql
里面有vc6工程,太久没有更新的我的vs无法打开工程。
我只编译了sqlite3的。mysql(其他)也是类似。
首先修改 Makefile.win.sqlite3 文件。
修改后打开vs命令行。 用 nmake /f Makefile.win.sqlite3 然后 nmake /f Makefile.win.sqlite3 install
luasql太久没更新了 里面有用到 luaL_openlib 函数,这函数好像5.2中就已经移除了 lua5.3 直接可能会编译不过。
方法一:添加 LUA_COMPAT_MODULE 预定义宏。兼容编译lua5.3的动态库。
方法二:在ls_sqlite3.c文件中添加几个函数
const char *luaL_findtable (lua_State *L, int idx,
const char *fname, int szhint)
{
const char *e;
if (idx) lua_pushvalue(L, idx);
do {
e = strchr(fname, '.');
if (e == NULL) e = fname + strlen(fname);
lua_pushlstring(L, fname, e - fname);
if (lua_rawget(L, -2) == LUA_TNIL) { /* no such field? */
lua_pop(L, 1); /* remove this nil */
lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */
lua_pushlstring(L, fname, e - fname);
lua_pushvalue(L, -2);
lua_settable(L, -4); /* set new table into field */
}
else if (!lua_istable(L, -1)) { /* field has a non-table value? */
lua_pop(L, 2); /* remove table and value */
return fname; /* return problematic part of the name */
}
lua_remove(L, -2); /* remove previous table */
fname = e + 1;
} while (*e == '.');
return NULL;
}
int libsize (const luaL_Reg *l) {
int size = 0;
for (; l && l->name; l++) size++;
return size;
}
void luaL_pushmodule (lua_State *L, const char *modname,
int sizehint)
{
luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); /* get _LOADED table */
if (lua_getfield(L, -1, modname) != LUA_TTABLE) { /* no _LOADED[modname]? */
lua_pop(L, 1); /* remove previous result */
/* try global variable (and create one if it does not exist) */
lua_pushglobaltable(L);
if (luaL_findtable(L, 0, modname, sizehint) != NULL)
luaL_error(L, "name conflict for module '%s'", modname);
lua_pushvalue(L, -1);
lua_setfield(L, -3, modname); /* _LOADED[modname] = new table */
}
lua_remove(L, -2); /* remove _LOADED table */
}
void luaL_openlib (lua_State *L, const char *libname,
const luaL_Reg *l, int nup) {
luaL_checkversion(L);
if (libname) {
luaL_pushmodule(L, libname, libsize(l)); /* get/create library table */
lua_insert(L, -(nup + 1)); /* move library table to below upvalues */
}
if (l)
luaL_setfuncs(L, l, nup);
else
lua_pop(L, nup); /* remove upvalues */
}
直接复制到文件中就是了。或者从你编译的lua的源文件中把
luaL_openlib
luaL_pushmodule
libsize
luaL_findtable
这四个函数复制到 ls_sqlite3.c的文件中
然后就可以 sqlite3 = require("luasql.sqlite3") 愉快的使用luasql了