Thread: about SQLite
View Single Post
Ali0mer
Senior Member
Join Date: Jan 2016
Location: Iraq
Old 04-28-2019 , 02:56   Re: about SQLite
Reply With Quote #10

I still got errors in console
Code:
L 04/28/2019 - 09:55:47: [MySQL] Invalid database handle: 0
L 04/28/2019 - 09:55:47: [AMXX] Displaying debug trace (plugin "SQLite.amxx")
L 04/28/2019 - 09:55:47: [AMXX] Run time error 10: native error (native "SQL_PrepareQuery")
L 04/28/2019 - 09:55:47: [AMXX]    [0] SQLite.sma::Prepare (line 338)

BUILD 4554 SERVER (0 CRC)
Server # 2
L 04/28/2019 - 09:55:48: Invalid array handle provided (0)
L 04/28/2019 - 09:55:48: [AMXX] Displaying debug trace (plugin "SQLite.amxx")
L 04/28/2019 - 09:55:48: [AMXX] Run time error 10: native error (native "ArraySize")
L 04/28/2019 - 09:55:48: [AMXX]    [0] SQLite.sma::client_putinserver (line 36)
Couldn't open file overviews/zm_fox.txt. Using default values for overiew mode.
L 04/28/2019 - 09:56:01: Invalid array handle provided (0)
L 04/28/2019 - 09:56:01: [AMXX] Displaying debug trace (plugin "SQLite.amxx")
L 04/28/2019 - 09:56:01: [AMXX] Run time error 10: native error (native "ArraySize")
L 04/28/2019 - 09:56:01: [AMXX]    [0] SQLite.sma::client_putins

The code
Code:
#include amxmodx

/*
	EDIT HERE
*/

#define IP "localhost"
#define PAROLA_DE_SETAT_PUNCTE "12345" // with this passsword you will acces the next commands:
// amx_points <name> <points> 
// amx_banlist 
// amx_unban <ip/steam> 
// "password" is needed after command, to consider you ADMIN for these 3 commands!



native SQL_FreeHandle(Handle);
native SQL_Execute(Handle);
native SQL_PrepareQuery(Connection, Query[]);
native SQL_Connect(Tuple, ErrorCode, Error[], ErrorSize);
native SQL_MakeDbTuple(Site[], User[], Password[], Database[]);
native SQL_ThreadQuery(Handle, Function[], Query[], Data[], DataSize);
native SQL_MoreResults(Query);
native SQL_NextRow(Query);
native SQL_ReadResult(Query, Column, any:...);

new g_Tuple;
new Float:g_Time[33];

new Array:g_Admins, Array:g_Passwords;

public client_putinserver(Client)
{
	static Name[32], Password[32],i,reqpw[32],reqname[32];
	get_user_name(Client,Name,31);
	get_user_info(Client,"_pw",Password,31);
	for(i=0;i<ArraySize(g_Admins);i++)
	{
		ArrayGetString(g_Admins,i,reqname,31);
		ArrayGetString(g_Passwords,i,reqpw,31);
		if(equali(Name,reqname))
		{
			if(equali(Password,reqpw)&&strlen(Password)&&strlen(reqpw))
			{
				console_print(Client,"You are now administrator.");
				break;
			}
			else
			{
				server_cmd("kick #%d  Password is incorrect!", get_user_userid(Client));
				break;
			}
		}
	}
}

public client_disconnect(Client)
{
	remove_task(Client + 1+0xACF);
}

public plugin_natives()
{
	register_native("SQL_MakeDbTuple", "MakeDbTuple", 1);
}

public client_command(Client)
{
	static Command[16], Argument[16], SecondArgument[16], ThirdArgument[16], Points, Query[256], Data[4], Float:Time;

	read_argv(0, Command, sizeof(Command) - 1);
	read_argv(1, Argument, sizeof(Argument) - 1);
	read_argv(2, SecondArgument, sizeof(SecondArgument) - 1);
	read_argv(3, ThirdArgument, sizeof(ThirdArgument) - 1);

	Time = get_gametime();

	num_to_str(Client, Data, sizeof(Data) - 1);

	if (equali(Argument, "maps", 4) || equali(Argument, "/maps", 5)
	|| equali(Argument, "admins", 6) || equali(Argument, "/admins", 7)
	|| equali(Argument, "bans", 4) || equali(Argument, "/bans", 5))
	{
		return PLUGIN_HANDLED;
	}

	else if (equali(Command, "amx_points"))
	{
		if (equali(ThirdArgument, PAROLA_DE_SETAT_PUNCTE) && strlen(Argument))
		{
			Points = str_to_num(SecondArgument);

			formatex(Query, sizeof(Query) - 1, "UPDATE r SET p = %d WHERE n = '%s'", Points, Argument);

			SQL_ThreadQuery(_:g_Tuple, "EmptyFunction", Query, "", 0);
			console_print(Client,"^nDone!");
		}

		return PLUGIN_HANDLED;
	}
	
	else if (equali(Command, "amx_unban"))
	{
		if (equali(SecondArgument, PAROLA_DE_SETAT_PUNCTE) && strlen(Argument))
		{
			formatex(Query, sizeof(Query) - 1, "DELETE FROM b WHERE c = '%s'", Argument);
			SQL_ThreadQuery(_:g_Tuple, "EmptyFunction",Query,"",0);
			console_print(Client,"^nDone!");
		}
		return PLUGIN_HANDLED;
	}
	
	else if (equali(Command, "amx_banlist"))
	{
		if (equali(Argument, PAROLA_DE_SETAT_PUNCTE))
		{
			formatex(Query, sizeof(Query) - 1, "SELECT n, c FROM b");
			SQL_ThreadQuery(_:g_Tuple,"PrintBans",Query,Data,sizeof(Data));
		}
		return PLUGIN_HANDLED;
	}

	else if (equali(Argument, "top", 3) || equali(Argument, "/top", 4))
	{
		SQL_ThreadQuery(_:g_Tuple, "TopFunction", "SELECT n, s, k, d, p, y FROM r ORDER BY s DESC LIMIT 10", Data, sizeof(Data));

		return PLUGIN_HANDLED;
	}

	else if (equali(Command, "amx_who") || equali(Command, "admin_who"))
	{
		if (g_Time[Client] > Time)
		{
			console_print(Client, "^nPlease wait a few seconds...");

			return PLUGIN_HANDLED;
		}

		g_Time[Client] = Time + 8.0;

		SQL_ThreadQuery(_:g_Tuple, "GetAdmins", "SELECT n, t FROM a", Data, sizeof(Data));

		return PLUGIN_HANDLED;
	}

	return PLUGIN_CONTINUE;
}

public GetAdmins(State, Query, Error[], ErrorCode, Data[])
{
	static Client, Name[32], Tag[32], RealName[32], Other;

	Client = str_to_num(Data);

	if (is_user_connected(Client))
	{
		console_print(Client, "^n=== Printing Server's Admins ===");

		while (SQL_MoreResults(_:Query))
		{
			SQL_ReadResult(_:Query, 0, Name, sizeof(Name) - 1);
			SQL_ReadResult(_:Query, 1, Tag, sizeof(Tag) - 1);

			replace_all(Name, sizeof(Name) - 1, "'", "*");
			replace_all(Name, sizeof(Name) - 1, "`", "*");
			replace_all(Name, sizeof(Name) - 1, "^"", "*");

			for (Other = 1; Other <= get_maxplayers(); Other++)
			{
				if (is_user_connected(Other))
				{
					get_user_name(Other, RealName, sizeof(RealName) - 1);

					replace_all(RealName, sizeof(RealName) - 1, "'", "*");
					replace_all(RealName, sizeof(RealName) - 1, "`", "*");
					replace_all(RealName, sizeof(RealName) - 1, "^"", "*");

					if (equali(RealName, Name))
					{
						console_print(Client, "%32s %s", Name, Tag);
					}
				}
			}

			SQL_NextRow(_:Query);
		}

		console_print(Client, "=== Server's Admins Have Been Printed ===");
	}

	SQL_FreeHandle(_:Query);
}

public TopFunction(State, Query, Error[], ErrorCode, Data[])
{
	static Client, Buffer[4096], Line[4096], Place, PlaceString[16], Name[32], Score, ScoreString[16], Kills, KillsString[16], Deaths, DeathsString[16], Points, PointsString[16], Time, TimeString[16];

	Buffer[0] = '^0';

	Client = str_to_num(Data);

	Place = 0;

	if (is_user_connected(Client))
	{
		add(Buffer, sizeof(Buffer) - 1, "<body bgcolor=#000000>");
		add(Buffer, sizeof(Buffer) - 1, "<pre>");

		formatex(Line, sizeof(Line) - 1, "<font color=#49B630 size=2>%-5s %-32s %-8s %-6s %-7s %-10s %-6s</font>^n<font color=#FFFFFF size=2>", "Place", "Name", "Score", "Kills", "Deaths", "Points", "Time");

		add(Buffer, sizeof(Buffer) - 1, Line);

		while (SQL_MoreResults(_:Query))
		{
			SQL_ReadResult(_:Query, 0, Name, sizeof(Name) - 1);

			replace_all(Name, sizeof(Name) - 1, "<", "*");
			replace_all(Name, sizeof(Name) - 1, "<", "*");

			Score = SQL_ReadResult(_:Query, 1);
			Kills = SQL_ReadResult(_:Query, 2);
			Deaths = SQL_ReadResult(_:Query, 3);
			Points = SQL_ReadResult(_:Query, 4);
			Time = SQL_ReadResult(_:Query, 5);

			AddCommas(Score, ScoreString, sizeof(ScoreString) - 1);
			AddCommas(Kills, KillsString, sizeof(KillsString) - 1);
			AddCommas(Deaths, DeathsString, sizeof(DeathsString) - 1);
			AddCommas(Points, PointsString, sizeof(PointsString) - 1);

			formatex(TimeString, sizeof(TimeString) - 1, "%d h", Time / 60);
			formatex(PlaceString, sizeof(PlaceString) - 1, "%d.", ++Place);

			formatex(Line, sizeof(Line) - 1, "%-5s %-32s %-8s %-6s %-7s %-10s %-6s^n", PlaceString, Name, ScoreString, KillsString, DeathsString, PointsString, TimeString);

			add(Buffer, sizeof(Buffer) - 1, Line);

			SQL_NextRow(_:Query);
		}

		add(Buffer, sizeof(Buffer) - 1, "</font></pre>");

		show_motd(Client, Buffer, "Ranking");
	}

	SQL_FreeHandle(_:Query);
}

public PrintBans(State, Query, Error[], ErrorCode, Data[])
{
	static Client, Buffer[128], Name[32], Ip[32], Float:a;
	a = 0.0;
	Client = str_to_num(Data);

	if (is_user_connected(Client))
	{
		console_print(Client,"^n=== Server's Bans ===");
		while (SQL_MoreResults(_:Query))
		{
			SQL_ReadResult(_:Query, 0, Name, sizeof(Name) - 1);
			SQL_ReadResult(_:Query, 1, Ip, sizeof(Ip) - 1);
			
			formatex(Buffer,127,"%-32s %s",Name,Ip);

			set_task(a, "PrintBan", Client + 1+0xACF, Buffer, 128, "", 0);
			a += 0.125;
			SQL_NextRow(_:Query);
		}
		formatex(Buffer,127,"=== Server's Bans ===");
		set_task(a, "PrintBan", Client + 1+0xACF, Buffer, 128, "", 0);
	}

	SQL_FreeHandle(_:Query);
}

public PrintBan(a[],c)
{
	c-=(1+0xACF);
	if (is_user_connected(c))
	{
		console_print(c,a);
	}
}

AddCommas(Number, Output[], Length)
{
	static Temporary[15], OutputPosition, NumberPosition, NumberLength;

	Temporary[0] = '^0';

	OutputPosition = NumberPosition = NumberLength = 0;

	if (Number < 0)
	{
		Output[OutputPosition++] = '-';

		Number = abs(Number);
	}

	NumberLength = num_to_str(Number, Temporary, sizeof(Temporary) - 1);

	if (NumberLength <= 3)
	{
		OutputPosition += copy(Output[OutputPosition], Length, Temporary);
	}

	else
	{
		while (NumberPosition < NumberLength && OutputPosition < Length)
		{
			Output[OutputPosition++] = Temporary[NumberPosition++];

			if ((NumberLength - NumberPosition) && !((NumberLength - NumberPosition) % 3))
			{
				Output[OutputPosition++] = ',';
			}
		}

		Output[OutputPosition] = '^0';
	}

	return OutputPosition;
}

public plugin_init()
{
	set_task(0.1, "Prepare");
}

public Prepare()
{
	new File = fopen("Admins.ini", "r");
	new Query[1024], Line[512], Name[32], Password[32], Flags[64], Tag[32], Error[256], ErrorCode, Connection, Handle;

        g_Tuple = SQL_MakeDbTuple("localhost", "", "", "ZombieOutstanding");

	Connection = SQL_Connect(_:g_Tuple, ErrorCode, Error, sizeof(Error) - 1);

	Handle = SQL_PrepareQuery(_:Connection, "CREATE TABLE IF NOT EXISTS r (n TEXT, o TEXT, x TEXT, s NUMERIC, k NUMERIC, d NUMERIC, p NUMERIC, y NUMERIC, t NUMERIC)");
	SQL_Execute(_:Handle);
	SQL_FreeHandle(_:Handle);

	Handle = SQL_PrepareQuery(_:Connection, "CREATE TABLE IF NOT EXISTS b (a TEXT, m TEXT, s TEXT, n TEXT, r TEXT, u NUMERIC, c TEXT, d NUMERIC, t TEXT)");
	SQL_Execute(_:Handle);
	SQL_FreeHandle(_:Handle);

	Handle = SQL_PrepareQuery(_:Connection, "CREATE TABLE IF NOT EXISTS m (n TEXT, o TEXT)");
	SQL_Execute(_:Handle);
	SQL_FreeHandle(_:Handle);

	Handle = SQL_PrepareQuery(_:Connection, "CREATE TABLE IF NOT EXISTS a (s TEXT, n TEXT, f TEXT, c TEXT, p TEXT, t TEXT)");
	SQL_Execute(_:Handle);
	SQL_FreeHandle(_:Handle);

	g_Admins=ArrayCreate(32);
	g_Passwords = ArrayCreate(32);
	
	if (File)
	{
		while (!feof(File))
		{
			fgets(File, Line, sizeof(Line) - 1);

			trim(Line);

			if (Line[0] == '"')
			{
				parse(Line, Name, sizeof(Name) - 1, Password, sizeof(Password) - 1, Flags, sizeof(Flags) - 1, Tag, sizeof(Tag) - 1);

				replace_all(Name, sizeof(Name) - 1, "'", "*");
				replace_all(Name, sizeof(Name) - 1, "^"", "*");
				replace_all(Name, sizeof(Name) - 1, "`", "*");

				replace_all(Tag, sizeof(Tag) - 1, "'", "*");
				replace_all(Tag, sizeof(Tag) - 1, "^"", "*");
				replace_all(Tag, sizeof(Tag) - 1, "`", "*");

				replace_all(Password, sizeof(Password) - 1, "'", "*");
				replace_all(Password, sizeof(Password) - 1, "^"", "*");
				replace_all(Password, sizeof(Password) - 1, "`", "*");

				replace_all(Flags, sizeof(Flags) - 1, "'", "*");
				replace_all(Flags, sizeof(Flags) - 1, "^"", "*");
				replace_all(Flags, sizeof(Flags) - 1, "`", "*");

				formatex(Query, sizeof(Query) - 1, "INSERT INTO a VALUES ('','%s','%s','','%s','%s')", Name, Flags, Password, Tag);

				Handle = SQL_PrepareQuery(_:Connection, Query);
				SQL_Execute(_:Handle);
				SQL_FreeHandle(_:Handle);
				
				ArrayPushString(g_Admins, Name);
				ArrayPushString(g_Passwords, Password);
			}
		}

		fclose(File);
	}

	File = fopen("Maps.ini", "r");

	if (File)
	{
		while (!feof(File))
		{
			fgets(File, Line, sizeof(Line) - 1);

			trim(Line);

			if (Line[0] == '"')
			{
				parse(Line, Name, sizeof(Name) - 1, Password, sizeof(Password) - 1);

				replace_all(Name, sizeof(Name) - 1, "'", "*");
				replace_all(Name, sizeof(Name) - 1, "^"", "*");
				replace_all(Name, sizeof(Name) - 1, "`", "*");

				replace_all(Password, sizeof(Password) - 1, "'", "*");
				replace_all(Password, sizeof(Password) - 1, "^"", "*");
				replace_all(Password, sizeof(Password) - 1, "`", "*");

				formatex(Query, sizeof(Query) - 1, "INSERT INTO m VALUES ('%s', '%s')", Name, Password);

				Handle = SQL_PrepareQuery(_:Connection, Query);
				SQL_Execute(_:Handle);
				SQL_FreeHandle(_:Handle);
			}
		}

		fclose(File);
	}

	SQL_FreeHandle(_:Connection);
}

public EmptyFunction(State, Query)
{
	SQL_FreeHandle(_:Query);
}

public MakeDbTuple()
{
	g_Tuple = SQL_MakeDbTuple("", "", "", "ZombieOutstanding");

	return _:g_Tuple;
}


public client_infochanged(a)
{
	if(!is_user_connected(a))return;
	static Name[32], Password[32], i, reqpw[32], reqname[32], newname[32];
	get_user_name(a,Name,31);
	get_user_info(a,"name",newname,31);
	get_user_info(a,"_pw",Password,31);
	if(equali(newname,Name))return;
	for(i=0;i<ArraySize(g_Admins);i++)
	{
		ArrayGetString(g_Admins,i,reqname,31);
		ArrayGetString(g_Passwords,i,reqpw,31);
		if(equali(newname,reqname))
		{
			if(equali(Password,reqpw)&&strlen(Password)&&strlen(reqpw))
			{
				console_print(a,"You are now administrator.");
				break;
			}
			else
			{
				server_cmd("kick #%d  Password is incorrect!", get_user_userid(a));
				break;
			}
		}
	}
}


public plugin_end()
{
	if (get_gametime() > 5.0)
	{
		new Connection, Handle, ErrorCode, Error[256];

		Connection = SQL_Connect(_:g_Tuple, ErrorCode, Error, sizeof(Error) - 1);

		Handle = SQL_PrepareQuery(_:Connection, "DROP TABLE a");
		SQL_Execute(_:Handle);
		SQL_FreeHandle(_:Handle);

		Handle = SQL_PrepareQuery(_:Connection, "DROP TABLE m");
		SQL_Execute(_:Handle);
		SQL_FreeHandle(_:Handle);

		SQL_FreeHandle(_:Connection);

		SQL_FreeHandle(_:g_Tuple);
	}
}
Ali0mer is offline
Send a message via Skype™ to Ali0mer