SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 01/12/2019, 01:21 PM   #1
Edvukas
Little Clucker
 
Join Date: Nov 2018
Posts: 18
Reputation: 0
Default I need help with updating mysql from R5 to R41-4

Hi, as you can see, I need help with updating MySQL version from R5 to R41-4. So how can I convert these to R41-4?

Code:
mysql_query( "SELECT ... FROM ... ORDER BY ..." );
Code:
mysql_store_result( );
while( mysql_fetch_row_format( savingstringexx ) )
	{
	    sscanf(savingstring,"p<|>ddffffffs[128]s[50]ddddddd",BLA, BLA, BLA );
Code:
if( mysql_fetch_row_format( strings ) )
	{
Code:
 mysql_free_result( );
Edvukas is offline   Reply With Quote
Old 01/12/2019, 01:47 PM   #2
Calisthenics
Gangsta
 
Join Date: May 2018
Posts: 953
Reputation: 167
Default Re: I need help with updating mysql from R5 to R41-4

Please stop removing the thread and re-posting it. I wrote an explanation and when I tried to preview it, it was gone. I had to re-write it for a second time. I tried to preview it again just now and the thread was removed again. If I did not copy it in clipboard, I would not write it for a third time..

All functions can be found here: https://wiki.sa-mp.com/wiki/MySQL

The connection handle returned by `mysql_connect` is necessary for other mysql functions:
pawn Code:
// global:
new MySQL: connectionHandle;

// in `OnGameModeInit`:
connectionHandle = mysql_connect(...);
You execute a threaded query and it requires a callback (public function) to fetch data from it.
pawn Code:
mysql_tquery(connectionHandle, "SELECT ... FROM ... ORDER BY ...", "OnSomethingLoad");
You can call cache functions in the specified callback:
pawn Code:
forward OnSomethingLoad();
public OnSomethingLoad()
{
    /*
        The equivalent function of `mysql_store_result` is `cache_save` but it is not needed unless you want to save the result for future usage (it is kept in memory).
        Function `mysql_fetch_row_format` moves to the next row.
        `cache_get_value*` functions have a `rowid` parameter (the first parameter).
        NOTE: row and columns indexes start from 0.
        When there are multiple rows, we use an iteration variable.
    */

    for (new i = 0, rows = cache_num_rows(); i < rows; i++)
    {
        /*
            sscanf splitting is not possible as the result is not returned in one line.
            a column index or column name has to be used instead in the `cache_get_value*`functions (the second parameter).
        */


        // you know the datatypes from the specifiers in sscanf line: "p<|>dd..."
        // "d" specifier is for integer, so you retrieve index 0 and index 1 as integers:
        cache_get_value_int(i, 0, BLA);
        cache_get_value_int(i, 1, BLA);

        // sscanf line: "p<|>ddffffff..."
        // "f" specifier is for integer, so you retrieve index 2, 3, 4, 5, 6 and 7 as floats:
        cache_get_value_float(i, 2, BLA);
        ...
        cache_get_value_float(i, 7, BLA);

        // sscanf line: "p<|>ddffffffs[128]s[50]..."
        // "s[size]" specifier is for strings, so you retrieve index 8 and index 9 as strings:
        // it accepts a last parameter for the length. It is required for enum-arrays and the default compiler.
        cache_get_value(i, 8, BLA, 128);
        cache_get_value(i, 9, BLA, 50);

        // the rest 7 columns are integers. From index 10 to 16.
        cache_get_value_int(i, 10, BLA);
        ...
        cache_get_value_int(i, 16, BLA);
    }
}
Calisthenics is offline   Reply With Quote
Old 02/12/2019, 10:06 PM   #3
Edvukas
Little Clucker
 
Join Date: Nov 2018
Posts: 18
Reputation: 0
Default Re: I need help with updating mysql from R5 to R41-4

I can't understand it:
pawn Code:
while( mysql_fetch_row_format( savingstringexx ) )
    {
pawn Code:
if( mysql_fetch_row_format( strings ) )
    {
how can I convert these to R41-4 and what does they do?
Edvukas is offline   Reply With Quote
Old 03/12/2019, 07:18 AM   #4
Calisthenics
Gangsta
 
Join Date: May 2018
Posts: 953
Reputation: 167
Default Re: I need help with updating mysql from R5 to R41-4

`mysql_fetch_row_format` fetches whole row from the result set and moves to the next row pointer (if any).

As I wrote in the comments, `cache_get_value*` functions have `rowid` as first parameter. When there is only 1 row returned, you pass 0 as `rowid`. However when there can be multiple rows, we use a loop.

pawn Code:
while( mysql_fetch_row_format( savingstringexx ) )
// is equivalent to:
for (new i = 0, rows = cache_num_rows(); i < rows; i++)
and the sscanf splitting is replaced with `cache_get_value*` functions as shown in the previous reply.
Calisthenics is offline   Reply With Quote
Old 03/12/2019, 05:42 PM   #5
Edvukas
Little Clucker
 
Join Date: Nov 2018
Posts: 18
Reputation: 0
Default Re: I need help with updating mysql from R5 to R41-4

also how can I convert this one? if( mysql_num_rows( ) ) or it is same as while( mysql_fetch_row_format( savingstringexx ) ) ?

in register/login system:
pawn Code:
if( mysql_num_rows( ) )
    {
        format( String, 512, "{FFFFFF}Player {FF0000}%s {FFFFFF}is registered\nIf you want to log in type password:", GetPlayerName(playerid));
        ShowPlayerDialog( playerid,2,DIALOG_STYLE_LOGIN,"Login",String,"Login","Exit" );
    }
    else
    {
        format( String, 512, "{FFFFFF}Player's {FF0000}%s {FFFFFF}registration.\nType password to register:", GetPlayerName(playerid));
        ShowPlayerDialog( playerid,1,DIALOG_STYLE_REGISTER,"Register",String,"Register","Exit" );
    }
Edvukas is offline   Reply With Quote
Old 03/12/2019, 06:49 PM   #6
Calisthenics
Gangsta
 
Join Date: May 2018
Posts: 953
Reputation: 167
Default Re: I need help with updating mysql from R5 to R41-4

`mysql_num_rows` can be replaced by either `cache_num_rows` (a function that returns the rows directly) or `cache_get_row_count` (which needs a variable to store the rows on and returns success/failure instead).
Calisthenics is offline   Reply With Quote
Old 03/12/2019, 10:25 PM   #7
Edvukas
Little Clucker
 
Join Date: Nov 2018
Posts: 18
Reputation: 0
Default Re: I need help with updating mysql from R5 to R41-4

how about
pawn Code:
mysql_retrieve_row( );
and
pawn Code:
mysql_fetch_field_row
? Sorry, I'm converting whole gamemode to R41-4. btw full code:
pawn Code:
format(Query, sizeof(Query), "SELECT * FROM `players` WHERE Name= '%s'", GetPlayerName(playerid));
    mysql_tquery(g_Sql, Query);
    mysql_retrieve_row( );
    mysql_fetch_field_row( fetch,"Skin" );          SAVESKIN[ playerid ] = strval(fetch);       SetPlayerSkin(playerid, SAVESKIN[ playerid ]);
    mysql_free_result();
Edvukas is offline   Reply With Quote
Old 04/12/2019, 07:48 AM   #8
Calisthenics
Gangsta
 
Join Date: May 2018
Posts: 953
Reputation: 167
Default Re: I need help with updating mysql from R5 to R41-4

`mysql_retrieve_row` moves to the next row pointer. In the code you posted, it does not make sense as only 1 row is returned. For multiple rows, replace it with the loop shown in the previous replies.

`mysql_fetch_field_row` fetches the data by its column name. Replace it with `cache_get_value_name_int`. Since only 1 row is returned, we said previously that `rowid` will be 0.
pawn Code:
cache_get_value_name_int(0, "Skin", SAVESKIN[ playerid ]);
// or overloaded macro that accepts either column index or column name:
// cache_get_value_int(0, "Skin", SAVESKIN[ playerid ]);

This is also wrong:
Code:
format(Query, sizeof(Query), "SELECT * FROM `players` WHERE Name= '%s'", GetPlayerName(playerid));
`GetPlayerName` does not return the name, it is passed by reference: https://wiki.sa-mp.com/wiki/Function:GetPlayerName
It should have given warning 202 for wrong number of arguments.
Calisthenics is offline   Reply With Quote
Old 06/12/2019, 12:24 PM   #9
Edvukas
Little Clucker
 
Join Date: Nov 2018
Posts: 18
Reputation: 0
Default Re: I need help with updating mysql from R5 to R41-4

pawn Code:
[15:31:08] [debug] Run time error 19: "File or function is not found"
[15:31:08] [debug]  cache_get_row_count
[15:31:08] [debug]  mysql_log
[15:31:08] [debug]  mysql_connect
[15:31:08] [debug]  mysql_errno
[15:31:08] [debug]  mysql_tquery
[15:31:08] [debug]  cache_get_value_index_int
[15:31:08] [debug]  cache_get_value_index_float
[15:31:08] [debug]  cache_get_value_index
[15:31:08] [debug]  mysql_format
[15:31:08] [debug]  mysql_escape_string
[15:31:08] [debug]  cache_get_value_name_int
[15:31:08] [debug] Run time error 19: "File or function is not found"
[15:31:08] [debug]  cache_get_row_count
[15:31:08] [debug]  mysql_log
[15:31:08] [debug]  mysql_connect
[15:31:08] [debug]  mysql_errno
[15:31:08] [debug]  mysql_tquery
[15:31:08] [debug]  cache_get_value_index_int
[15:31:08] [debug]  cache_get_value_index_float
[15:31:08] [debug]  cache_get_value_index
[15:31:08] [debug]  mysql_format
[15:31:08] [debug]  mysql_escape_string
[15:31:08] [debug]  cache_get_value_name_int
[15:31:08] Script[gamemodes/gm.amx]: Run time error 19: "File or function is not found"
[15:31:08] Number of vehicle models: 0
pawn Code:
[15:31:07]  Loading plugin: mysql.so
[15:31:07]   Failed (libmysqlclient.so.18: cannot open shared object file: No such file or directory)
[15:31:07]  Loading plugin: libmySQL.so
[15:31:07]   Failed (plugins/libmySQL.so: invalid ELF header)
[15:31:07]  Loading plugin: streamer.so
Edvukas is offline   Reply With Quote
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mysql not updating a row. thimo Scripting Help 2 21/01/2014 03:17 PM
MySQL not updating SomebodyAndMe Scripting Help 2 10/06/2012 11:00 AM
MYSQL not updating? Nuke547 Scripting Help 3 17/04/2012 02:43 AM
Updating a mysql row ihatetn931 Help Archive 0 24/11/2010 12:53 PM


All times are GMT. The time now is 11:01 PM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.