Raised This Month: $12 Target: $400
 3% 

MYSQL - My first plugin


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Grafi_
Junior Member
Join Date: Apr 2022
Location: Poland
Old 04-26-2022 , 14:51   MYSQL - My first plugin
Reply With Quote #1

Hi, i want to create my advertisement plugin with database.
I would like to make it work so that the plugin sends chat messages that can be changed in the database, but I don't know how to start writing a plugin with database. Do you have any tutorials?

Last edited by Grafi_; 04-26-2022 at 14:52.
Grafi_ is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 04-26-2022 , 15:19   Re: MYSQL - My first plugin
Reply With Quote #2

https://wiki.alliedmods.net/SQL_(SourceMod_Scripting)

*mention, do you have own database (MySQL) ?
And have you ever done SQL commands ?
__________________
Do not Private Message @me

Last edited by Bacardi; 04-26-2022 at 15:20.
Bacardi is offline
Grafi_
Junior Member
Join Date: Apr 2022
Location: Poland
Old 04-26-2022 , 15:30   Re: MYSQL - My first plugin
Reply With Quote #3

Quote:
Originally Posted by Bacardi View Post
*mention, do you have own database (MySQL) ?
You mean do I have a database management tool? If so, I don't really have one XD

Quote:
Originally Posted by Bacardi View Post
And have you ever done SQL commands ?
No
Grafi_ is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 04-26-2022 , 16:21   Re: MYSQL - My first plugin
Reply With Quote #4

With dedicated MySQL host, you can share database with multiple game servers.
And easier way to use with PHP web pages etc. etc.
- Do you host more than one SRCDS ?

Sourcemod can run local SQL database called SQLite, it cannot connect to others server, only to one.
- You need program or web-browser-addon to open SQLite database file to edit. Which is some how tedious.
- SQLite has some differences from MySQL commands.

Then there is KeyValue files, you can edit with normal text editor but you need keep structure rigth.
Look like this:
https://github.com/ErikMinekus/sm-ad...rtisements.txt

So, do you still want to use database or would it be easier to use txt file (KeyValues) ?
__________________
Do not Private Message @me
Bacardi is offline
Grafi_
Junior Member
Join Date: Apr 2022
Location: Poland
Old 04-27-2022 , 10:33   Re: MYSQL - My first plugin
Reply With Quote #5

Quote:
Originally Posted by Bacardi View Post
With dedicated MySQL host, you can share database with multiple game servers.
And easier way to use with PHP web pages etc. etc.
- Do you host more than one SRCDS ?

Sourcemod can run local SQL database called SQLite, it cannot connect to others server, only to one.
- You need program or web-browser-addon to open SQLite database file to edit. Which is some how tedious.
- SQLite has some differences from MySQL commands.

Then there is KeyValue files, you can edit with normal text editor but you need keep structure rigth.
Look like this:
https://github.com/ErikMinekus/sm-ad...rtisements.txt

So, do you still want to use database or would it be easier to use txt file (KeyValues) ?
Yes i still want database because on 3 servers i will be able to change messages and add them more easily.
Grafi_ is offline
Grafi_
Junior Member
Join Date: Apr 2022
Location: Poland
Old 04-27-2022 , 12:19   Re: MYSQL - My first plugin
Reply With Quote #6

Quote:
Originally Posted by Grafi_ View Post
Yes i still want database because on 3 servers i will be able to change messages and add them more easily.
HTML Code:
#include <sourcemod>
#include <sdktools>

Database g_hDB;

public void OnPluginStart()
{
    SQL_TConnect(Connect, "advertisement");
}

public void OnMapStart()
{
    timer = CreateTimer(45.0, MessageRepeat, _, TIMER_REPEAT)
}

public void Connect(Database db, const char[] error, any data)
{
	if(db == null)
		SetFailState("Unable to connect to database: %s", error);
	
	g_hDB = db;
	
	char sQuery[256];
	g_hDB.Format(sQuery, sizeof(sQuery), "CREATE TABLE IF NOT EXISTS `advertisement` ('message' varchar(256) NOT NULL)");
	g_hDB.Query(OnSQLConnectCallback, sQuery);
}

public void OnSQLConnectCallback(Database db, DBResultSet results, const char[] error, any data)
{
	if (results == null)
	{
		LogError("Query failure: %s", error);
		return;
	}
}

public Action MessageRepeat(Handle timer, any data)
{ 
    char sBuffer[256];
    Format(sBuffer, sizeof(sBuffer), "SELECT 'message' FROM 'advertisement'")
    DBResultSet query = SQL_TQUERY(db, sBuffer)
    if (query == null)
    {
        char sError[256];
        SQL_GetError(db, sError, sizeof(sError))
        PrintToServer("Error %s", sError)
    }
    else
    {
        // *I'd like to have something like this here someday* printochat(message from database) *messages displayed one by one from the database)
    }
}

public void Check(Database db, DBResultSet results, const char[] error, any data)
{
	if (results == null)
		LogError("Query failure: %s", error);
}
I know it is very weak but I tried to do something, could you advise me what I should do? XD
And I know the code is full of bugs

Last edited by Grafi_; 04-27-2022 at 12:21.
Grafi_ is offline
Bacardi
Veteran Member
Join Date: Jan 2010
Location: mom's basement
Old 04-28-2022 , 17:12   Re: MYSQL - My first plugin
Reply With Quote #7

...here something.

- You need configure databases.cfg
Code:
"Databases"
{
	"driver_default"		"mysql"
	
	// When specifying "host", you may use an IP address, a hostname, or a socket file path
	
	"default"
	{
		"driver"			"default"
		"host"				"localhost"
		"database"			"sourcemod"
		"user"				"root"
		"pass"				""
		//"timeout"			"0"
		//"port"			"0"
	}
	
	"storage-local"
	{
		"driver"			"sqlite"
		"database"			"sourcemod-local"
	}

	"clientprefs"
	{
		"driver"			"sqlite"
		"host"				"localhost"
		"database"			"clientprefs-sqlite"
		"user"				"root"
		"pass"				""
		//"timeout"			"0"
		//"port"			"0"
	}

	"my_connect_configure"
	{
		"driver"			"mysql"
		"host"				"localhost"
		"database"			"database_test"
		"user"				"root"
		"pass"				""
		//"timeout"			"0"
		//"port"			"0"
	}
}
- Plugin create connection, everytime when map start.
After database queries, it close connection.
= You can however, create connection once and leave it open. It's up to you.

- Plugin is looking (and create table if query result is null) table called "table_advertisement"
And add first message when table is created.

- Plugin print all messages from table, into server console
Attached Files
File Type: sp Get Plugin or Get Source (test.sp - 69 views - 2.9 KB)
__________________
Do not Private Message @me
Bacardi is offline
Austin
Senior Member
Join Date: Oct 2005
Old 04-29-2022 , 03:14   Re: MYSQL - My first plugin
Reply With Quote #8

https://sqlitebrowser.org/dl/
Austin is offline
KateUhlerredalfias
New Member
Join Date: Apr 2022
Old 05-11-2022 , 08:05   Re: MYSQL - My first plugin
Reply With Quote #9

As someone who's written a storage engine plugin, I've found that executing a query from within a MySQL plugin is incredibly difficult. MySQL isn't re-entrant, due to locking within the MySQL process. You could use the MySQL client api (as suggested by chris) to connect to the same server. But it is 99% likely you will simply deadlock the whole omegle.2yu.co server.

Last edited by KateUhlerredalfias; 07-20-2022 at 03:48.
KateUhlerredalfias is offline
Rohanlogs
Senior Member
Join Date: Nov 2015
Old 05-14-2022 , 17:17   Re: MYSQL - My first plugin
Reply With Quote #10

Quote:
Originally Posted by KateUhlerredalfias View Post
As someone who's written a storage engine plugin, I've found that executing a query from within a MySQL plugin is incredibly difficult. MySQL isn't re-entrant, due to locking within the MySQL process. You could use the MySQL client api (as suggested by chris) to connect to the same server. But it is 99% likely you will simply deadlock the whole server.
I dunno what you mean, MySQL works just fine with sourcemod even with some complex queries.
Its far more superior in data storing than anything else in my opinion when you're dealing with a lot of data. Of course depends what kind of plugin you're doing but still..
Its not that difficult, just organize your code well. The OPs question is quite simple.
He just wants to pull chat advertisements from the database which Bacardi gave an example script of already.

There's no deadlocking involved with such a simple plugin unless your code is somehow really bad..
You aren't doing that much inserting/updating from multiple sessions in this case that'd cause a deadlock in this situation.
InnoDB also detects deadlocks by default and rolls back the transactions to break the lock.
__________________
Rohanlogs is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 20:21.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode