forked from cfadmin-cn/cfadmin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_DB.lua
More file actions
139 lines (118 loc) · 3.14 KB
/
test_DB.lua
File metadata and controls
139 lines (118 loc) · 3.14 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
local DB = require "DB"
local co = require "internal.Co"
require "utils"
local ok, err = DB.init({
host = "localhost",
port = 3306,
database = "test",
user = "root",
password = "123456789"
})
if not ok then
return print("连接mysql 失败: "..err)
end
--[[
复制下面语句到任意管理工具即可导入测试表进行测试
SET NAMES utf8;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for `user`
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL,
`user` varchar(255) NOT NULL,
`passwd` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4;
SET FOREIGN_KEY_CHECKS = 1;
--]]
-- 插入语句示例
co.spwan(function ( ... )
local ret, err = DB.insert("user")
:fields({"name", "user", "passwd"})
:values({
{"candy", "root", "123456789"},
{"水果糖", "admin", "123456789"},
})
:execute()
if not ret then
return print(err)
end
var_dump(ret)
end)
-- 查询语句示例
co.spwan(function ( ... )
local ret, err = DB.select({"id", "name", "user", "passwd"})
:from({"user"})
:where({
{"id", "!=", "0"},
"AND",
{"id", ">=", "1"},
"OR",
{"user", "!=", "admin"},
"AND",
{"user", "=", "admin"},
"OR",
{"user", "IS", "NOT", "NULL"},
"AND",
{"user", "IS", "NULL"},
"AND",
{"user", "IN", {1, 2, 3, 4, 5}},
"AND",
{"user", "NOT", "IN", {1, 2, 3, 4, 5}},
"AND",
{"user", "BETWEEN", {1, 100}},
"AND",
{"user", "NOT", "BETWEEN", {1, 100}},
})
:groupby('id') -- groupby({"name", "user"})
:orderby("id") -- orderby({"name", "user"})
:asc() -- or desc()
:limit(1) -- limit("1") limit(1, 100)
:execute() -- 所有语句最后必须指定这个方法才会真正执行
if not ret then
return print(err)
end
var_dump(ret)
end)
-- 更新语句示例
co.spwan(function ( ... )
local ret, err = DB.update("user")
:set({
{"name", "=", "管理员"},
{"user", "=", "Administrator"},
{"passwd", "=", "Administrator"},
})
:where({
{"id", "<=", 1},
})
:limit(1)
:execute()
if not ret then
return print(err)
end
var_dump(ret)
end)
-- 删除语句示例
co.spwan(function ( ... )
local ret, err = DB.delete("user")
:where({
{"id", ">", 1},
})
:orderby("id")
:limit(1)
:execute()
if not ret then
return print(err)
end
var_dump(ret)
end)
co.spwan(function ( ... )
local ret, err = DB.query("show variables like 'wait_timeout'")
if not ret then
return print(err)
end
var_dump(ret)
end)