AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting (https://forums.alliedmods.net/forumdisplay.php?f=107)
-   -   Problem MySQL T_LoadData (https://forums.alliedmods.net/showthread.php?t=318222)

PyKw 08-20-2019 13:03

Problem MySQL T_LoadData
 
Any idea what I'm doing wrong here?

Code:

#include <sourcemod>
int valoare[MAXPLAYERS + 1] = {0, ...};
Database g_Database = null;

public Plugin myinfo =
{
        name = "Test Plugin",
        author = "Picutz",
        description = "",
        version = "1.0",
        url = "http://steamcommunity.com/profiles/76561198453137840"
}

public OnPluginStart()
{
        RegConsoleCmd("sm_setvaloare", SetValoare);
        Database.Connect(T_Connect);
}

public Action SetValoare(client, args)
{
        char arg[64];
        GetCmdArg(1, arg, sizeof(arg));
        valoare[client] = StringToInt(arg);
        PrintToChat(client, "Ai setat valoarea pe %d", StringToInt(arg));
}

public void T_Connect(Database db, const char[] error, any data)
{
    if(db == null)
    {
        LogError("Database failure: %s", error);
    }
        else
        {
                g_Database = db;
        }
}

public void OnClientAuthorized(int client)
{
    //idk if querying from a steamid of BOT is a great idea
    if(!IsFakeClient(client) && g_Database != null)
    {
        char query[256], steamid[32];
        GetClientAuthId(client, AuthId_Steam2, steamid, sizeof(steamid));
        FormatEx(query, sizeof(query), "SELECT * FROM jailbreak_test WHERE steamid = '%s'", steamid);
        g_Database.Query(T_LoadData, query, GetClientUserId(client));

        //TQuery will send the following to the threaded callback:
        //Database = db
        //Results = results
    }
}

public void OnClientDisconnect(int client)
{
        char query[256], steamid[32];
        GetClientAuthId(client, AuthId_Steam2, steamid, sizeof(steamid));
        FormatEx(query, sizeof(query), "UPDATE jailbreak_test SET valoare = %i WHERE steamid = '%s'", valoare[client], steamid);
        g_Database.Query(T_LoadData, query, GetClientUserId(client));
}

public void T_LoadData(Database db, DBResultSet results, const char[] error, any data)
{
    //If either are broken:
    if(db == null || results == null)
    {
        LogError("T_LoadData returned error: %s", error);
        return;
    }

    int client = GetClientOfUserId(data);

    int valoareCol;
    results.FieldNameToNum("valoare", valoareCol);

    //If a row set was returned:
    if(results.FetchRow())
    {
        valoare[client] = results.FetchInt(valoareCol);
    }
    else
    {
        //not found in db, insert data:
        char steamid[32]; GetClientAuthId(client, AuthId_Steam2, steamid, sizeof(steamid));
        char query[256]; Format(query, sizeof(query), "INSERT INTO jailbreak_test (steamid) VALUES ('%s') ON DUPLICATE KEY UPDATE steamid = '%s';", steamid, steamid);
        db.Query(T_Generic, query);
    }
}

//THIS IS OUR OTHER COOL CALLBACK TO JUST DO NICE QUERIES WITHOUT RETURNED DATA!
public void T_Generic(Database db, DBResultSet results, const char[] error, any data)
{
    if(db == null || results == null)
    {
        LogError("T_Generic returned error: %s", error);
        return;
    }
}

I get error in logs:
Code:

L 08/20/2019 - 19:43:27: [SM] Exception reported: No current result set
L 08/20/2019 - 19:43:27: [SM] Blaming: testNewDb.smx
L 08/20/2019 - 19:43:27: [SM] Call stack trace:
L 08/20/2019 - 19:43:27: [SM]  [0] DBResultSet.FieldNameToNum
L 08/20/2019 - 19:43:27: [SM]  [1] Line 77, testNewDb.sp::T_LoadData

Line 77:
Quote:

results.FieldNameToNum("valoare", valoareCol);
Database: https://imgur.com/a/4hMjbJe

GetRektByNoob 08-20-2019 13:29

Re: Problem MySQL T_LoadData
 
can i ask why are you doing
Code:

results.FieldNameToNum("valoare", valoareCol);
and not
Code:

results.FieldNameToNum("valoare", 0);
since you are defining the int but not doing anything to it
also you posted half of the error, it should be something like this
Code:

L 08/20/2019 - 19:43:27: [SM] Exception reported: [Reason]
L 08/20/2019 - 19:43:27: [SM] Blaming: testNewDb.smx
L 08/20/2019 - 19:43:27: [SM] Call stack trace:
L 08/20/2019 - 19:43:27: [SM]  [0] DBResultSet.FieldNameToNum
L 08/20/2019 - 19:43:27: [SM]  [1] Line 77, testNewDb.sp::T_LoadData


PyKw 08-20-2019 13:55

Re: Problem MySQL T_LoadData
 
Quote:

Originally Posted by GetRektByNoob (Post 2663972)
can i ask why are you doing
Code:

results.FieldNameToNum("valoare", valoareCol);
and not
Code:

results.FieldNameToNum("valoare", 0);
since you are defining the int but not doing anything to it
also you posted half of the error, it should be something like this
Code:

L 08/20/2019 - 19:43:27: [SM] Exception reported: [Reason]
L 08/20/2019 - 19:43:27: [SM] Blaming: testNewDb.smx
L 08/20/2019 - 19:43:27: [SM] Call stack trace:
L 08/20/2019 - 19:43:27: [SM]  [0] DBResultSet.FieldNameToNum
L 08/20/2019 - 19:43:27: [SM]  [1] Line 77, testNewDb.sp::T_LoadData


My bad, i edited the error. And I'm using that because I need to store the field index in a variable.
https://sm.alliedmods.net/new-api/db...FieldNameToNum

GetRektByNoob 08-20-2019 14:22

Re: Problem MySQL T_LoadData
 
afaik you are trying to find the field index but the result is not fetched try doing
Code:

//If a row set was returned:
    if(results.FetchRow())
    {
        int valoareCol;
            results.FieldNameToNum("valoare", valoareCol);
        valoare[client] = results.FetchInt(valoareCol);
    }
    else
    {
        //not found in db, insert data:
        char steamid[32]; GetClientAuthId(client, AuthId_Steam2, steamid, sizeof(steamid));
        char query[256]; Format(query, sizeof(query), "INSERT INTO jailbreak_test (steamid) VALUES ('%s') ON DUPLICATE KEY UPDATE steamid = '%s';", steamid, steamid);
        db.Query(T_Generic, query);
    }


PyKw 08-20-2019 18:49

Re: Problem MySQL T_LoadData
 
Quote:

Originally Posted by GetRektByNoob (Post 2663990)
afaik you are trying to find the field index but the result is not fetched try doing
Code:

//If a row set was returned:
    if(results.FetchRow())
    {
        int valoareCol;
            results.FieldNameToNum("valoare", valoareCol);
        valoare[client] = results.FetchInt(valoareCol);
    }
    else
    {
        //not found in db, insert data:
        char steamid[32]; GetClientAuthId(client, AuthId_Steam2, steamid, sizeof(steamid));
        char query[256]; Format(query, sizeof(query), "INSERT INTO jailbreak_test (steamid) VALUES ('%s') ON DUPLICATE KEY UPDATE steamid = '%s';", steamid, steamid);
        db.Query(T_Generic, query);
    }


Now I'm getting this error:
Code:

L 08/21/2019 - 01:46:35: [SM] Exception reported: No current result set
L 08/21/2019 - 01:46:35: [SM] Blaming: testNewDb.smx
L 08/21/2019 - 01:46:35: [SM] Call stack trace:
L 08/21/2019 - 01:46:35: [SM]  [0] DBResultSet.FetchRow
L 08/21/2019 - 01:46:35: [SM]  [1] Line 84, testNewDb.sp::T_LoadData


GetRektByNoob 08-20-2019 20:02

Re: Problem MySQL T_LoadData
 
I'm sorry for the mistake but the error comes from the sql statement when doing update you dont actually select any field and which means the query table is empty and you can't find the field index

you need to use the select * statement instead

PyKw 08-21-2019 15:00

Re: Problem MySQL T_LoadData
 
Quote:

Originally Posted by GetRektByNoob (Post 2664037)
I'm sorry for the mistake but the error comes from the sql statement when doing update you dont actually select any field and which means the query table is empty and you can't find the field index

you need to use the select * statement instead

Can you please write it down, cause I don't get it!

ShD3luxe 08-21-2019 20:54

Re: Problem MySQL T_LoadData
 
Code:

       
public void OnClientDisconnect(int client)
{
        char query[256], steamid[32];
        GetClientAuthId(client, AuthId_Steam2, steamid, sizeof(steamid));
        FormatEx(query, sizeof(query), "UPDATE jailbreak_test SET valoare = %i WHERE steamid = '%s'", valoare[client], steamid);
        g_Database.Query(T_LoadData, query, GetClientUserId(client));
}

Update does not return a result set .
Use fast query for UPDATE,INSERT,DELETE (operations without a result set).
Code:

public void OnClientDisconnect(int client)
{
        char query[256], steamid[32];
        GetClientAuthId(client, AuthId_Steam2, steamid, sizeof(steamid));
        FormatEx(query, sizeof(query), "UPDATE jailbreak_test SET valoare = %i WHERE steamid = '%s'", valoare[client], steamid);
        SQL_LockDatabase(db);// lock the database until the update operation is done bcs you are using threaded operations with non-threaded
        bool execQueryResult = SQL_FastQuery(db, query);
        SQL_UnlockDatabase(db); // unlock it right after the operation is done to prevent a deadlock
        if (!execQueryResult) // returns true if the operation was successful , false if there was a query problem or invalid db handle
        {
                char error[255];
                SQL_GetError(db, error, sizeof(error));
                PrintToServer("Failed to query (error: %s)", error);
        }
}

Example from here -> https://wiki.alliedmods.net/SQL_(SourceMod_Scripting
Fast query doc -> https://sm.alliedmods.net/new-api/dbi/SQL_FastQuery
Note: SQL_FastQuery is a non-threaded querie so keep that in mind when you are using it with threaded queries.

or change the line
Code:

g_Database.Query(T_LoadData, query, GetClientUserId(client));  //from OnClientDisconnect where you have the update query

--->

db.Query(T_Generic, query);

Since you create the entry for the user when he is connected if he is not in the table (so u can just update it bcs it exists)
Also make sure to have a default column value for 'valoare' , maybe 0 since you store points or credits.

GetRektByNoob 08-22-2019 05:08

Re: Problem MySQL T_LoadData
 
Quote:

Originally Posted by ShD3luxe (Post 2664155)
Code:

       
public void OnClientDisconnect(int client)
{
        char query[256], steamid[32];
        GetClientAuthId(client, AuthId_Steam2, steamid, sizeof(steamid));
        FormatEx(query, sizeof(query), "UPDATE jailbreak_test SET valoare = %i WHERE steamid = '%s'", valoare[client], steamid);
        g_Database.Query(T_LoadData, query, GetClientUserId(client));
}

Update does not return a result set .
Use fast query for UPDATE,INSERT,DELETE (operations without a result set).
Code:

public void OnClientDisconnect(int client)
{
        char query[256], steamid[32];
        GetClientAuthId(client, AuthId_Steam2, steamid, sizeof(steamid));
        FormatEx(query, sizeof(query), "UPDATE jailbreak_test SET valoare = %i WHERE steamid = '%s'", valoare[client], steamid);
        SQL_LockDatabase(db);// lock the database until the update operation is done bcs you are using threaded operations with non-threaded
        bool execQueryResult = SQL_FastQuery(db, query);
        SQL_UnlockDatabase(db); // unlock it right after the operation is done to prevent a deadlock
        if (!execQueryResult) // returns true if the operation was successful , false if there was a query problem or invalid db handle
        {
                char error[255];
                SQL_GetError(db, error, sizeof(error));
                PrintToServer("Failed to query (error: %s)", error);
        }
}

Example from here -> https://wiki.alliedmods.net/SQL_(SourceMod_Scripting
Fast query doc -> https://sm.alliedmods.net/new-api/dbi/SQL_FastQuery
Note: SQL_FastQuery is a non-threaded querie so keep that in mind when you are using it with threaded queries.

or change the line
Code:

g_Database.Query(T_LoadData, query, GetClientUserId(client));  //from OnClientDisconnect where you have the update query

--->

db.Query(T_Generic, query);

Since you create the entry for the user when he is connected if he is not in the table (so u can just update it bcs it exists)
Also make sure to have a default column value for 'valoare' , maybe 0 since you store points or credits.

its always better to use the method map in general (Database.Query...) and not the query functions like FastQuery because then you dont have to use functions like GetError LockDatabase... and you dont have to worry about errors coming from not locking the database or something

ShD3luxe 08-22-2019 05:42

Re: Problem MySQL T_LoadData
 
For a simple non-threaded database I prefer FastQuery , in his example mixing both might be a bad idea but that's a solution too .


All times are GMT -4. The time now is 23:49.

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