· 6 years ago · Jul 10, 2019, 09:56 AM
1#!/usr/bin/lua5.1
2
3dofile('config.lua')
4luasql = require "luasql.sqlite3"
5socket = require("socket")
6env = luasql.sqlite3()
7conn = env:connect(cfg.path)
8
9-- create a TCP socket and bind it to the host and port
10local server = socket.bind(cfg.ip, cfg.port)
11
12-- create table in db "data"
13
14conn:execute("create table if not exists data(id INTEGER PRIMARY KEY AUTOINCREMENT, data_id INTEGER UNIQUE, value TEXT NOT NULL, timestamp INTEGER NOT NULL)")
15
16cursor = conn:execute("SELECT * FROM data")
17
18
19function split(datatosplit)
20 tmptbl = {};
21 for match in (datatosplit .. "/"):gmatch("(.-)/") do
22 table.insert(tmptbl, match);
23 end
24 return tmptbl;
25end
26
27
28while true do
29 client=server:accept()
30 print ("Client connected")
31
32 while true do
33 client:send("waiting for command..." .. "\n")
34 local command = client:receive()
35 if string.find(command,"get|#%d+|") == 1 then
36 local tblofsplit = split (command)
37 tblofsplit[2] = string.gsub (tblofsplit[2],"#","",1)
38 client:send(tblofsplit[2] .. "\n")
39 elseif string.find(command,"set|#%d+|number=%d;string=%w+|") == 1 then
40 conn:execute("insert into data values(8,10,'tesst',15)")
41-- client:send(table.concat(row, '|') .. "\n")
42 client:send("ok")
43 elseif string.find(command,"del/#%d+/") == 1 then
44 local tblofsplit = split (command)
45 tblofsplit[2] = string.gsub (tblofsplit[2],"#","",1)
46-- client:send(table.concat(row, '|') .. "\n")
47 client:send(tblofsplit[2] .. "\n")
48 elseif string.find(command,"quit") == 1 then
49 client:close()
50 break
51 else
52 client:send("incorrect command" .. "\n")
53 end
54end
55end
56
57
58
59client:close()
60cursor:close()
61conn:close()
62env:close()