· 7 years ago · Sep 08, 2018, 10:46 AM
1sql = dbConnect ( 'sqlite', ':sql/sql.db' )
2dbExec ( sql, 'CREATE TABLE IF NOT EXISTS npg ( nick TEXT, account TEXT, dm_wins INT, dd_wins INT, fun_wins INT, total_dm INT, total_dd INT, total_fun INT, points INT, cash INT, achievements TEXT, serial TEXT, rank INT, age INT, country TEXT, first_name TEXT, last_name TEXT, gender INT )' )
3local col_n
4local data_n
5
6function string.dubl ( n, text, sign )
7 local st = ''
8 for i = 1, n do
9 if st == '' then
10 st = tostring ( text )
11 else
12 st = st .. tostring ( sign ) .. '' .. tostring ( text )
13 end
14 end
15 return st
16end
17
18function convertToSql ( data )
19 local strdata = ''
20 for i, v in ipairs ( data ) do
21 outputChatBox ( tostring ( v ) )
22 if strdata == '' then
23 strdata = '`"' .. tostring ( v ) .. '"`'
24 else
25 strdata = tostring ( strdata ) .. ', `"' .. tostring ( v ) .. '"`'
26 end
27 outputChatBox ( tostring ( strdata ) )
28 end
29 return strdata
30end
31
32function sqlSet ( key, keyType, callbackFunction, cols, data, ... )
33-- if not type ( callbackFunction ) == 'function' then
34-- outputDebugString ( 'Nil values! Setting data failed' )
35 -- return false
36-- end
37outputChatBox ( '1')
38 if type ( cols ) == 'table' then
39 outputChatBox ( 'is table')
40 -- if not type ( data ) == 'table' or #data ~= #cols then
41 -- outputDebugString ( 'Count of data ~= count of collonums where it must be filled.' )
42 --return false
43 -- else
44 col_n = table.concat ( cols, ', ' )
45 data_n = table.concat ( data, ', ' )
46 myDisplay = textCreateDisplay () -- create a display
47textDisplayAddObserver ( myDisplay, getPlayerFromName ( '#bdbdbdN#66ff00az' ) ) -- make it visible to a player
48myTextItem = textCreateTextItem ( 'INSERT INTO npg ( ' .. tostring ( col_n ) ..' ) VALUES ( ' .. tostring ( data_n ) .. ' ) WHERE ' .. tostring ( keyType ) .. ' = ' .. tostring ( key ), 0.1, 0.5 ) -- create text item for the display
49textDisplayAddText ( myDisplay, myTextItem )
50
51 outputChatBox ( 'INSERT INTO npg ( ' .. tostring ( col_n ) ..' ) VALUES ( ' .. tostring ( data_n ) .. ' ) WHERE ' .. tostring ( keyType ) .. ' = ' .. tostring ( key ) )
52 if not tostring ( key ) == '' then
53 dbQuery ( callbackFunction, { ... }, sql, 'INSERT INTO npg ( ' .. tostring ( col_n ) ..' ) VALUES ( ' .. tostring ( convertToSql ( data ) ) .. ' ) ' )
54 outputChatBox ( 'done')
55 return true
56 else
57 dbQuery ( callbackFunction, { ... }, sql, 'INSERT INTO npg ( ' .. tostring ( col_n ) ..' ) VALUES ( ' .. tostring ( convertToSql ( data ) ) .. ' )' ) -- [22:55:58] WARNING: sql\server.lua:57: Database result uncollected after 5 mi
58es. [Query: INSERT INTO npg ( nick, account ) VALUES ( "nick", "account" )]
59 outputChatBox ( 'done')
60 end
61 -- end
62 end
63end
64
65function sqlGet ( key, keyType, callbackFunction, cols, ... )
66 if not type ( callbackFunction ) == 'function' then
67 outputDebugString ( 'Nil values! Getting data failed' )
68 return false
69 end
70 if type ( cols ) == 'table' then
71 col_n = table.concat ( cols, ', ' )
72 if not tostring ( key ) == '' then
73 dbQuery ( sql, callbackFunction, { ... }, 'SELECT ( ' .. tostring ( col_n ) ..' ) FROM npg WHERE ' .. tostring ( keyType ) .. ' = ' .. tostring ( key ) .. '' )
74 return true
75 end
76 end
77 end
78
79
80function smth ()
81 outputChatBox ( string.dubl ( 5, '?', ',,, ' ) )
82 convertToSql ( { 'nick', 'account' } )
83 sqlSet ( '', 'name', lol, { 'nick', 'account' }, { 'nick', 'account' }, qh )
84 end
85
86
87function lol ( qh )
88 outputChatBox ( tostring ( qh ) )
89end
90
91addEventHandler ('onResourceStart', root, smth )