-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLuaLoad.c
More file actions
70 lines (63 loc) · 1.42 KB
/
LuaLoad.c
File metadata and controls
70 lines (63 loc) · 1.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "LuaLoad.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lualib.h"
#include "lauxlib.h"
#include "sqlite3.h"
#include "lua.h"
int lua_callback(void *state, int argc, char **argv, char **names)
{
int i, count;
count = lua_tointeger(state, -1);
lua_newtable(state);
for (i = 0; i < argc; i++)
{
lua_pushstring(state, names[i]);
lua_pushstring(state, argv[i]);
lua_settable(state, -3);
}
lua_settable(state, -3);
count++;
lua_pushinteger(state, count);
return 0;
}
int lua_sqlite_open(lua_State *state)
{
sqlite3 *db;
char *file_name = lua_tostring(state, -1);
if (sqlite3_open(file_name, &db))
{
lua_pushstring(state, sqlite3_errmsg(db));
sqlite3_close(db);
lua_error(state);
}
lua_pushlightuserdata(state, db);
return 1;
}
int lua_sqlite_close(lua_State *state)
{
sqlite3 *db = (sqlite3 *)lua_touserdata(state, 1);
if (sqlite3_close(db))
{
luaL_error(state, "Can not close Database");
}
return 1;
}
int lua_sqlite_exec(lua_State *state)
{
const char *query = lua_tostring(state, 2);
sqlite3 *db = (sqlite3*)lua_touserdata(state, 1);
char *zErrMsg;
lua_newtable(state);
lua_pushinteger(state, 1);
if (sqlite3_exec(db, query, lua_callback, state, &zErrMsg))
{
lua_pushfstring(state, "%s\n", zErrMsg);
sqlite3_free(zErrMsg);
sqlite3_close(db);
lua_error(state);
}
lua_pop(state, 1);
return 1;
}