[Plugin] [REL] MySQL Plugin (Now on github!)

Updating from R5-6-7 to R39-2 require a lot of modifications : the syntax of natives has changed, most of the natives of R5-6-7 were removed and replaced by better/faster/easier equivalents.
Reply

Quote:
Originally Posted by IKnowWhatIAmDoing
Посмотреть сообщение
I want to update to R39-2 But I got a lot of errors, can someone tell me what should I replace these with?
You don't know what you're doing.
Reply

Quote:
Originally Posted by iZN
Посмотреть сообщение
You don't know what you're doing.
Yeah I may try this on my own but for example I couldn't find a replacement for mysql_num_rows, store, free result
Reply

Get started by learning how threaded queries work: https://sampforum.blast.hk/showthread.php?tid=337810
The Wiki will help you for the functions: https://sampwiki.blast.hk/wiki/MySQL/R33
And an example script: https://github.com/pBlueG/SA-MP-MySQ...stem-cache.pwn
Reply

I cant install the boost libraries (version 1.55 or higher) Is it absolutely necesary?
Reply

maddinat0r what's the alternative for mysql_fetch_row in R33+, I am currently using R39-2.
Though I have worked out something and it is working perfectly fine but still I'd like to know if there is one.
Reply

I've added some time ago some functions for the sake of compatibility, one of them was a mysql_fetch_row_format function. You can find that function and more in R37's a_mysql.inc file, at the bottom.
I'm not really sure if this is a good written function, but I think it should do what the respecting one in R6 did.
Reply

Just a quick question regarding multiple queries, had a look but with 500 pages and the search not giving me a lot of advice, thought I would just ask!

I've got a command that finds storage located in businesses and once I have the ID of the storage, I run another query to get the name of the resource that is stored inside of the storage:
pawn Code:
COMMAND:bstorage(playerid, params[])
{
    new tBID, tRID, query[100], string[256];
    if(sscanf(params, "d", tBID)){ return SendClientMessage(playerid, -1, "USAGE:\"/bstorage [Business IDl]\""); }
    mysql_format(mysql, query, sizeof(query), "SELECT * from `businesses_storage` where `BID` = '%d'", tBID);
    mysql_query(mysql, query);
    if(cache_num_rows())
    {
        for(new i = 0; i<cache_num_rows(); i++)
        {
            tRID = cache_get_field_content_int(i, "RID", mysql);
            format(string, sizeof(string), "RID: %d - %s", tRID, GetResourceName(tRID));
            SCM(playerid, -1, string);
        }
    }
    return 1;
}

stock GetResourceName(RID)
{
    new
        query[100],
        tempName[24];
    mysql_format(mysql2, query, sizeof(query), "SELECT `Name` FROM `resources` WHERE `ID` = '%d'", RID);
    mysql_query(mysql2, query);
    if(cache_num_rows())
    {
        cache_get_field_content(0, "Name", tempName, mysql2);
    }
    return tempName;
}
It sort of works, but it only returns the first result:
However if I remove the GetResourceName from the format line it returns:

I have the two different mysql connections and run both of the queries on the different connections. I have also made sure that they are using different connection IDs.

Any ideas?
Reply

There is a conflict with those two caches. The cached result generated in your helper function (GetResourceName) overwrites the active cache you generated at the top of your command.
You also don't need two different connections for this, just make sure the caches don't overwrite themselves.
Oh, and you forgot to delete the cache. Here are some more informations on mysql_query.
Here's the fixed version of your code:
Code:
COMMAND:bstorage(playerid, params[])
{
    new tBID, tRID, query[100], string[256];
    if(sscanf(params, "d", tBID)){ return SendClientMessage(playerid, -1, "USAGE:\"/bstorage [Business IDl]\""); }
    mysql_format(mysql, query, sizeof(query), "SELECT * from `businesses_storage` where `BID` = '%d'", tBID);
    new Cache:res1 = mysql_query(mysql, query);
	//res1 is now the active cache here
	for(new i = 0; i < cache_num_rows(); i++)
	{
		tRID = cache_get_field_content_int(i, "RID");
		format(string, sizeof(string), "RID: %d - %s", tRID, GetResourceName(tRID));
		SCM(playerid, -1, string);
		//the cache in your function "GetResourceName" overwrote our "res1" cache, 
		//we need to manually set "res1" as the active cache now
		cache_set_active(res1);
	}
    
	cache_delete(res1);
    return 1;
}

stock GetResourceName(RID)
{
    new
        query[100],
        tempName[24];
    mysql_format(mysql, query, sizeof(query), "SELECT `Name` FROM `resources` WHERE `ID` = '%d'", RID);
    new Cache:result = mysql_query(mysql, query);
	//result is now the active cache here, overwriting any previously active cache
    if(cache_num_rows())
    {
        cache_get_field_content(0, "Name", tempName);
    }
	cache_delete(result);
	//result has been deleted, there is now no active cache
    return tempName;
}
Reply

Thanks for the link, will check that out. Copied the code across and it was still only showing the first line. However I added the mysql2 connection handle inside GetResourceName and it worked? Any reason why it would work like that and not without the mysql2 connection handle?

EDIT: I think some of the lines, such as cahce_delete didn't have the mysql connection handle, replaced it with mysql from mysql2 and it is working still! Thanks for your help!!
Reply

fails
Reply

Any one getting this error while running server
Code:
Failed (libmysqlclient_r.so.15: cannot open shared object file: No such file or directory)
Then here is the solution:-

HTML Code:
Step 1:- wget http://downloads.mysql.com/archives/mysq...006888Step 2:- rpm -qlp MySQL-shared-compat-5.1.49-1.rhel5.i386.rpm
Reply

Hello, I've got 2 questions.

First, what is the difference between mysql_pquery() and mysql_tquery()? In wiki it says both are multithreaded.

Secoond, What is field id? Is it a column of result set? How do you know field ID of result set?
Reply

Quote:
Originally Posted by SupremeCommander
View Post
Hello, I've got 2 questions.

First, what is the difference between mysql_pquery() and mysql_tquery()? In wiki it says both are multithreaded.

Secoond, What is field id? Is it a column of result set? How do you know field ID of result set?
1. I've also asked that question few months ago, http://forum.sa-mp.com/converse.php?u=96478&u2=96708

2. Detail about how MySQL uses indexes: http://dev.mysql.com/doc/refman/4.1/...l-indexes.html

Use cache_get_field_count to get the field count from the current result set.

If you're going to use cache_get_row_*, field index always starts from 0.
Reply

Quote:
Originally Posted by iZN
View Post
1. I've also asked that question few months ago, http://forum.sa-mp.com/converse.php?u=96478&u2=96708

2. Detail about how MySQL uses indexes: http://dev.mysql.com/doc/refman/4.1/...l-indexes.html

Use cache_get_field_count to get the field count from the current result set.

If you're going to use cache_get_row_*, field index always starts from 0.
Thanks for the answer.

So mysql_pquery is parallel query.. How does it work? Does it execute query and calls callback at same time or does it call the callback after executing the query?
Reply

I just cannot get it to work. Even with only a single testing filterscript and an empty gamemode it refuses to return any results. I suspect because I only have 1 core to my disposition on this VPS. Non-threaded queries work fine.

Edit: I reinstalled the OS from the VPS CP. Now using 7.3 rather than 7.6 and it seems to be in working order now. So yeah. Touch wood.
Reply

Hi, I don't understand why I got a "[04:10:36] [WARNING] cache_get_row_count - no active cache" when this code executes itself.

Mysql Log file:
Код:
[04:08:36] [DEBUG] mysql_tquery - connection: 1, query: "SELECT * FROM `voitures`", callback: "(null)", format: "(null)"
[04:08:36] [DEBUG] cache_get_row_count - connection: 1
[04:08:36] [WARNING] cache_get_row_count - no active cache
Code:
Код:
public SauvegardeVoitures()
{
	new query[128];
	format(query, sizeof(query), "SELECT * FROM `voitures`");
	mysql_tquery(mysql, query, "");
	new nbVoitures =  cache_get_row_count();
	printf("Dйbut save v, nbVoitures = %d", nbVoitures);
	if(nbVoitures > 0)
	{
		for(new v = 1; v < nbVoitures; v++)
		{	
			SauvegardeVoiture(v);
			printf("Save veh: %d", v);
		}
	}
	return 1;
}
This code is supposed to find in the database how much vehicles there is, then save them.
But actually, nbVoitures is always 0 what ever there's in database.


Anybody know how could I fix this please ?


Thank you
Reply

@Anou : To use cache functions in your code after a mysql_tquery, you MUST specify a callback in parameter of mysql_tquery.

Doing like this is the good way :

pawn Код:
forward myCallback();
public myCallback()
{
      new count = cache_get_row_count();
      // etc
}

// in some function

mysql_tquery(1, "SELECT * FROM myTable", "myCallback");
Whereas doing like this isn't :

pawn Код:
mysql_tquery(1, "SELECT * FROM myTable");
new count = cache_get_row_count();
// etc
The problem is that the cache is turned on only when the callback is called.
Reply

Really good plugin.
Reply

The links for R7 and below are dead!
Reply


Forum Jump:


Users browsing this thread: 21 Guest(s)