· 8 years ago · Mar 26, 2018, 07:54 PM
1void SQL_ConnectDB()
2{
3 DB = SQL_Connect("banksystem", true, sError, sizeof(sError));
4 if (DB == null)
5 {
6 SetFailState("Could not connect to database, error: %s", sError);
7 }
8}
9void SQL_CreateTable()
10{
11 SQL_TQuery(DB, SQLErrorCallback, "CREATE TABLE IF NOT EXISTS bankusers (steamid varchar(32) NOT NULL PRIMARY KEY, cash integer(32) NOT NULL, bankmoney integer(32) NOT NULL)");
12}
13public void SQLErrorCallback(Handle owner, Handle hndl, const char[] error, any:data)
14{
15 if (!StrEqual("", error))
16 {
17 LogError("Query failed: %s", error);
18 }
19}
20void SQL_UpdateMoney(int money, int bankmoney, const char[] auth)
21{
22 char[] sQuery = new char[150];
23 FormatEx(sQuery, 150, "DELETE FROM bankusers WHERE steamid='%s'", auth);
24
25 SQL_LockDatabase(DB);
26 SQL_FastQuery(DB, sQuery);
27 SQL_UnlockDatabase(DB);
28
29 char[] query = new char[150];
30 FormatEx(query, 150, "INSERT INTO bankusers (steamid, cash, bankmoney) VALUES ('%s', '%d', '%d')", auth, money, bankmoney);
31
32 SQL_LockDatabase(DB);
33 SQL_FastQuery(DB, query);
34 SQL_UnlockDatabase(DB);
35}
36void SQL_CheckMoney(int client, const char[] auth)
37{
38 char[] sQuery = new char[150];
39 FormatEx(sQuery, 150, "SELECT cash, bankmoney FROM bankusers WHERE steamid = '%s'", auth);
40 DB.Query(SQL_CheckMoney_Callback, sQuery, client, DBPrio_Low);
41}
42public void SQL_CheckMoney_Callback(Database db, DBResultSet results, const char[] error, any data)
43{
44 int client = data;
45 if (!IsClientAuthorized(client))
46 {
47 return;
48 }
49
50 if (results == null)
51 {
52 LogError("DB Query error: %s", error);
53 return;
54 }
55 while (results.FetchRow())
56 {
57 int money = results.FetchInt(0);
58 int bank = results.FetchInt(1);
59 clientmoney[client] = money;
60 clientbankmoney[client] = bank;
61 CPrintToChat(client, "[{green}Bank{default}]Welcome back {green}%N{default}, you have {green}$%d {default}in your bank and {green}$%d {default}in your packet!", client, clientmoney[client], clientbankmoney[client]);
62 }
63}