Mysql R8 - loading multiple rows
#1

Did a extensive search on the forums, most posts were from earlier versions and did not help. I am using BlueG's R8 Mysql Plugin (r14).

Here is how I am loading the banks:
pawn Код:
public thread_loadbank(playerid)
{
    new rows, fields;
    cache_get_data(rows, fields);
    if(rows)
    {
        cache_get_row(0, 0, bInfo[playerid][bOwner], connectionHandle, 24);
        cache_get_row(0, 1, bInfo[playerid][bNickname], connectionHandle, 50);
        bInfo[playerid][bAccountNumber] = cache_get_row_int(0, 2, connectionHandle);
        bInfo[playerid][bPin] = cache_get_row_int(0, 3, connectionHandle);
        bInfo[playerid][bType] = cache_get_row_int(0, 4, connectionHandle);
        cache_get_row(0, 5, bInfo[playerid][bCorp], connectionHandle, 50);
        bInfo[playerid][bMoney] = cache_get_row_int(0, 6, connectionHandle);
        bInfo[playerid][bMaxWithdraw] = cache_get_row_int(0, 7, connectionHandle);
       
        cache_get_row(1, 0, bInfo2[playerid][bOwner], connectionHandle, 24);
        cache_get_row(1, 1, bInfo2[playerid][bNickname], connectionHandle, 50);
        bInfo2[playerid][bAccountNumber] = cache_get_row_int(1, 2, connectionHandle);
        bInfo2[playerid][bPin] = cache_get_row_int(1, 3, connectionHandle);
        bInfo2[playerid][bType] = cache_get_row_int(1, 4, connectionHandle);
        cache_get_row(1, 5, bInfo2[playerid][bCorp], connectionHandle, 50);
        bInfo2[playerid][bMoney] = cache_get_row_int(1, 6, connectionHandle);
        bInfo2[playerid][bMaxWithdraw] = cache_get_row_int(1, 7, connectionHandle);

// MORE CODE DOWN HERE HOWEVER MY MAIN QUESTION IS ABOVE
I have a feeling I am doing it way wrong, and I am guessing I do not even need to create new variables for each different row, but it was the only thing I could think of doing.

Basically what this does is you can create multiple bank accounts and I am trying to have it so it loads them all when you connect.
Reply
#2

24 hour thread bump
Reply
#3

I am seeing a lot of "while" statements made with ealier versions using the mysql_get_row_format. Does this work with R8 and caching?
Reply
#4

Take a look at the syntax for the cache_get_row() function.

The first argument is to determine the row to select information from. The second argument is to determine which field to select information from, and obviously, the third is which var to store the information into.

So, if you wanted to do something like "SELECT * FROM `houses`" and load up all of the houses in your database, you need to make a loop. Like this:

pawn Код:
// ...

cache_get_data(rows, fields);

if(rows > 0)
{
    for(new i = 0; i < rows; i++)
    {
        cache_get_row(i, 0, temp);
        // ...
    }
}

// ...
Reply
#5

Thanks for the reply

However, I was playing around with it a little more, if I am saving it to multiple different variables (Bankaccount1, bankaccount2, bankaccount3, etc) wouldn't looping it be ineffective? As when it loads row 1, it will save it to bankaccount1, and then when it loops to row 2, it will still be saving it to bankaccount1, in a sense overwriting what was there before.
Reply
#6

Say you had a vehicle enum that you wanted to load and store data into. If the enum was empty, it would simply be 20x easier to just load each row from the DB and use the indexing variable (in the example I gave you, it was "i") as the vehicle's enum ID. Here's an example...

pawn Код:
enum vehicleInfo
{
    vID,
    vModel,
    Float:vPosX
};

new
    vInfo[MAX_VEHICLES][vehicleInfo];
   
public thread_LoadVehicles()
{
    new
        rows,
        fields;
   
    cache_get_data(rows, fields);
   
    if(rows > 0) // checking to see if there are even any rows at all
    {
        for(new i = 0; i < rows; i++) // creating a loop that will execute depending on how many rows are returned
        {
            cache_get_row(i, 0, temp); // load field 0's info
                vInfo[i][vID] = strval(temp); // store field 0's info into the vID part of the enum; if "i" = 0, then the "enum ID" would also be 0
            cache_get_row(i, 1, temp); // same as above, different field
                vInfo[i][vModel] = strval(temp); // same as above, different part of the enum
            cache_get_row(i, 2, temp); // same as above, different field
                vInfo[i][vPosX] = floatstr(temp); // same as above, different part of the enum
        }
    }
   
    // at this point, all vehicles are loaded
    return 1;
}
That's about the most efficient way I can think of...
Reply
#7

pawn Код:
for(new i = 0; i < rows; i++)
        {
            SendClientMessage(playerid, -1, "Running the LOOP!");
            strdel(bInfo[i][bNick], 0, 50);
            cache_get_row(i, 1, bInfo[i][bNick], connectionHandle, 50);
            bInfo[i][bMaxWithdraw] = cache_get_row_int(i, 7, connectionHandle);
        }
I am using R8 V14 for this, so the syntax is a little different than R7. However, I went ahead and created a few command to see what is on what row, through a little guesswork I came up with this:
pawn Код:
CMD:row0(playerid, params[])
{
    new msg[250];
    format(msg, sizeof(msg), "%s is row 0", bInfo[0][bNick]);
    SendClientMessage(playerid, -1, msg);
    return 1;
}
Instead of using "bInfo[playerid][bNick]" I changed playerid with "0", assuming that because we are using "i" in the loop it is giving each one an ID. Is this correct?

Also it seems to be only running the loop once, then the server crashes.
Reply
#8

You would be correct about being able to replace [playerid] with [0]. As the "i" begins with 0 in the loop, the "enum ID" will also begin with 0.

As far as the crash goes, is there a connection issue with the database?
Reply
#9

No, there isn't any connection issue, as other MySQL queries are running fine.

Here is the crash report:
Код:
[10:22:55] [debug] Server crashed while executing bank.amx
[10:22:55] [debug] AMX backtrace:
[10:22:55] [debug] #0 native cache_get_row_int () [005a1d00] from mysql.so
[10:22:55] [debug] #1 000080ac in public thread_loadbank () from bank.amx
[10:22:55] [debug] Native backtrace:
[10:22:55] [debug] #0 00a03e0b in _ZN10StackTraceC1EPv () from plugins/crashdetect.so
[10:22:55] [debug] #1 009fde7f in _ZN11CrashDetect20PrintNativeBacktraceEPv () from plugins/crashdetect.so
[10:22:55] [debug] #2 009fe956 in _ZN11CrashDetect11OnExceptionEPv () from plugins/crashdetect.so
[10:22:55] [debug] #3 00a03b1c in ?? () from plugins/crashdetect.so
[10:22:55] [debug] #4 0090e410 in ?? ()
[10:22:55] [debug] #5 003eaabc in ?? () from /lib/libc.so.6
[10:22:55] [debug] #6 003ea820 in strtol () from /lib/libc.so.6
[10:22:55] [debug] #7 005a1d63 in _ZN7Natives19n_cache_get_row_intEP6tagAMXPi () from plugins/mysql.so
[10:22:55] [debug] #8 08093c94 in ?? () from /home/tcagame/Admin/1/samp03svr
[10:22:55] [debug] #9 009fc60b in _ZN11CrashDetect13DoAmxCallbackEiPiS0_ () from plugins/crashdetect.so
[10:22:55] [debug] #10 00a00208 in ?? () from plugins/crashdetect.so
[10:22:55] [debug] #11 00a05f43 in amx_Exec () from plugins/crashdetect.so
[10:22:55] [debug] #12 009fdd2a in _ZN11CrashDetect9DoAmxExecEPii () from plugins/crashdetect.so
[10:22:55] [debug] #13 00a002a9 in ?? () from plugins/crashdetect.so
[10:22:55] [debug] #14 005a55d9 in ProcessTick () from plugins/mysql.so
[10:22:55] [debug] #15 080c5df2 in ?? () from /home/tcagame/Admin/1/samp03svr
[10:22:55] [debug] #16 080b5b7a in ?? () from /home/tcagame/Admin/1/samp03svr
[10:22:55] [debug] #17 080b1453 in ?? () from /home/tcagame/Admin/1/samp03svr
[10:22:55] [debug] #18 003d1ce6 in __libc_start_main () from /lib/libc.so.6
[10:22:55] [debug] #19 0804b521 in ?? () from /home/tcagame/Admin/1/samp03svr
Reply
#10

That seems like a bunch of gibberish to me. Post that on the crashdetect plugin release topic so ZeeX can de-code it for you!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)