· 8 years ago · Jun 01, 2018, 09:48 PM
1-------------------------------
2sql = {
3 host = '127.0.0.1',
4 port = '3306',
5 user = 'mta',
6 pass = '------',
7 database = 'mta'
8}
9-------------------------------
10local db = {}
11db.connection = dbConnect('mysql','dbname='..sql.database..';host='..sql.host..';charset=utf8',sql.user,sql.pass,'share=0')
12
13function db.query(callback,query,...)
14 if query and type(query) == 'string' and db.connection then
15 dbQuery(callback,db.connection,query,unpack(arg))
16 end
17end
18
19function db.createTable(name,arguments)
20 if name and type(name) == 'string' and arguments and type(arguments) == 'string' and db.connection then
21 local args = split(arguments,',')
22 local s = 'CREATE TABLE IF NOT EXISTS `??` ('
23 local add = {}
24 local bools = {}
25 for i=1,#args do
26 local v = split(args[i],' ')
27 if i == 1 then
28 table.insert(add,'`??` '..v[2])
29 else
30 table.insert(add,',`??` '..v[2])
31 end
32 table.insert(bools,v[1])
33 end
34 local newS = s..table.concat(add,'')..');'
35 dbExec(db.connection,newS,name,unpack(bools))
36 end
37end
38db.createTable('players','name VARCHAR(255),serial VARCHAR(255),ip VARCHAR(255)')
39
40function db.add(name,c,v)
41 if name and type(name) == 'string' and c and type(c) == 'string' and v and type(v) == 'string' and db.connection then
42 local s = 'REPLACE INTO `??` ('
43 local columns,values = split(c,','),split(v,',')
44 local columnStrings,valueStrings = {},{}
45 local pack = {}
46
47 for i=1,#columns do
48 if i == 1 then
49 table.insert(columnStrings,'`??`')
50 table.insert(valueStrings,'?')
51 else
52 table.insert(columnStrings,',`??`')
53 table.insert(valueStrings,',?')
54 end
55 table.insert(pack,columns[i])
56 end
57
58 for e=1,#values do
59 table.insert(pack,values[e])
60 end
61
62 local newS = s..table.concat(columnStrings,'')..') VALUES ('..table.concat(valueStrings)..');'
63 dbExec(db.connection,newS,name,unpack(pack))
64 end
65end
66db.add('players','name,serial,ip','dawg,no,127.0.0.1')