· 7 years ago · Jan 02, 2019, 01:10 AM
1#include <sourcemod>
2#pragma semicolon 1
3
4
5
6// Handle for the db
7new Handle:db;
8
9
10
11public OnPluginStart()
12{
13 // Testcommand
14 RegConsoleCmd("sm_dbtest", Command_Test);
15
16 // Create database if it doesn't exist and create rhe table
17 InitDB(db);
18}
19
20
21
22InitDB(&Handle:DbHNDL)
23{
24
25 // Errormessage Buffer
26 new String:Error[255];
27
28 // COnnect to the DB
29 DbHNDL = SQL_Connect("zhell_test", true, Error, sizeof(Error));
30
31
32 // If something fails we quit
33 if(DbHNDL == INVALID_HANDLE)
34 {
35 SetFailState(Error);
36 }
37
38 // Querystring
39 new String:Query[255];
40 Format(Query, sizeof(Query), "CREATE TABLE IF NOT EXISTS players (steamid TEXT UNIQUE, name TEXT);");
41
42 // Database lock
43 SQL_LockDatabase(DbHNDL);
44
45 // Execute the query
46 SQL_FastQuery(DbHNDL, Query);
47
48 // Database unlock
49 SQL_UnlockDatabase(DbHNDL);
50
51}
52
53
54
55
56public Action:Command_Test(client, args)
57{
58 // Use our function to write something to the DB
59
60 char name[32];
61 char auth[32];
62 GetClientAuthId(client, AuthId_SteamID64, auth, sizeof(auth), false);
63 GetClientName(client, name, sizeof(name));
64
65 AddPlayer(auth, name);
66 return Plugin_Handled;
67}
68
69
70
71
72// Function to add something to the DB
73AddPlayer(char auth[32], char name[32])
74{
75 // We could just write the query in the function but i like it that way
76 new String:Query[255];
77 Format(Query, sizeof(Query), "INSERT OR IGNORE INTO players VALUES ('%s', '%s')", auth, name);
78
79 // Send our Query to the Function
80 SQL_TQuery(db, SQL_ErrorCheckCallBack, Query);
81}
82
83
84
85
86// Function to update/rename something in the DB
87UpdatePlayer(const String:Id[21], const String:Name[32])
88{
89 // We could just write the query in the function but i like it that way
90 new String:Query[255];
91 Format(Query, sizeof(Query), "UPDATE players SET name = '%s' WHERE steamid = '%s'", Name, Id);
92
93 // Send our Query to the Function
94 SQL_TQuery(db, SQL_ErrorCheckCallBack, Query);
95}
96
97
98
99
100ReadAll()
101{
102 new String:Query[255];
103 Format(Query, sizeof(Query), "SELECT * FROM players");
104
105 // Send our Query to the Function
106 SQL_TQuery(db, SQL_ReadAll, Query);
107}
108
109
110
111
112
113public SQL_ReadAll(Handle:owner, Handle:hndl, const String:error[], any:data)
114{
115 // We need to know the rowcount, in this case 2-1 because field begins at 0
116 new RowCount = SQL_GetFieldCount(hndl);
117
118 // Temp, just for debugging or so
119 new field;
120
121 // Buffer for our result
122 new String:Buffer[255];
123
124 // We need to fetch each row to get the results of it
125 while(SQL_FetchRow(hndl))
126 {
127 // For every row one go
128 for(new i; i< RowCount; i++)
129 {
130 // Gets the String from field i
131 SQL_FetchString(hndl, i, Buffer, sizeof(Buffer));
132 PrintToServer("Field %d | Row: %d | String: %s", field, i, Buffer);
133 }
134 // Increment our fieldcount
135 field++;
136 }
137}
138
139
140
141
142
143public SQL_ErrorCheckCallBack(Handle:owner, Handle:hndl, const String:error[], any:data)
144{
145 // This is just an errorcallback for function who normally don't return any data
146 if(hndl == INVALID_HANDLE)
147 {
148 SetFailState("Query failed! %s", error);
149 }
150}