-
Notifications
You must be signed in to change notification settings - Fork 9
/
mysql_part.cpp
85 lines (69 loc) · 1.72 KB
/
mysql_part.cpp
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include "mysql_part.h"
#include "log.h"
#include "interface_c.h"
extern "C"
{
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
extern lua_State* L;
CMysql::CMysql()
{
}
CMysql::~CMysql()
{
}
int
CMysql::connect_mysql(const char* host,
const char* user,
const char* password,
const char* dbname,
unsigned int port)
{
mysql_conn = new CMysqlConnect();
if(!mysql_conn->Connect(host, user, password, dbname, port, "", "utf8")) {
log_error("connect mysql failed\n");
return -1;
}
mysql_store = new CMysqlStore();
mysql_store->SetTransAction(mysql_conn);
return 0;
}
int
CMysql::query(const char* sql)
{
if(sql == "") return 0;
if (mysql_conn->GetConnect() == NULL) {
m_strError = mysql_conn->What();
log_error("mysql query error, reason:%s", m_strError.c_str());
return -1;
}
if(mysql_store->Query(sql))
{
unsigned long count = mysql_store->RowCount();
if (count <= 0) {
return 0;
}
lua_newtable(L);
for(unsigned int i=0; i<count; i++)
{
int colcount = mysql_store->GetColCount();
lua_pushinteger(L, i+1);
lua_newtable(L);
for(int j=0; j<colcount;j++)
{
string val = mysql_store->GetItemString(i, j);
lua_pushinteger(L, j+1);
lua_pushstring(L, val.c_str());
lua_rawset(L, -3);
}
lua_rawset(L, -3);
}
lua_setglobal(L, MYSQL_RESULT_SET);
return 0;
}
m_strError = mysql_store->What();
log_error("mysql query error, reason:%s", m_strError.c_str());
return -1;
}