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

Problem MySQL T_LoadData


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
PyKw
Junior Member
Join Date: Jan 2012
Old 08-20-2019 , 13:03   Problem MySQL T_LoadData
Reply With Quote #1

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

Last edited by DarkDeviL; 03-23-2021 at 03:49. Reason: Restore to previous version.
PyKw is offline
GetRektByNoob
Member
Join Date: Nov 2018
Old 08-20-2019 , 13:29   Re: Problem MySQL T_LoadData
Reply With Quote #2

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

Last edited by GetRektByNoob; 08-20-2019 at 13:30.
GetRektByNoob is offline
PyKw
Junior Member
Join Date: Jan 2012
Old 08-20-2019 , 13:55   Re: Problem MySQL T_LoadData
Reply With Quote #3

Quote:
Originally Posted by GetRektByNoob View Post
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
PyKw is offline
GetRektByNoob
Member
Join Date: Nov 2018
Old 08-20-2019 , 14:22   Re: Problem MySQL T_LoadData
Reply With Quote #4

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);
    }

Last edited by GetRektByNoob; 08-20-2019 at 14:22.
GetRektByNoob is offline
PyKw
Junior Member
Join Date: Jan 2012
Old 08-20-2019 , 18:49   Re: Problem MySQL T_LoadData
Reply With Quote #5

Quote:
Originally Posted by GetRektByNoob View Post
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
PyKw is offline
GetRektByNoob
Member
Join Date: Nov 2018
Old 08-20-2019 , 20:02   Re: Problem MySQL T_LoadData
Reply With Quote #6

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
GetRektByNoob is offline
PyKw
Junior Member
Join Date: Jan 2012
Old 08-21-2019 , 15:00   Re: Problem MySQL T_LoadData
Reply With Quote #7

Quote:
Originally Posted by GetRektByNoob View Post
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!
PyKw is offline
ShD3luxe
Member
Join Date: Aug 2019
Location: Localhost
Old 08-21-2019 , 20:54   Re: Problem MySQL T_LoadData
Reply With Quote #8

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.

Last edited by ShD3luxe; 08-21-2019 at 22:28. Reason: doc
ShD3luxe is offline
GetRektByNoob
Member
Join Date: Nov 2018
Old 08-22-2019 , 05:08   Re: Problem MySQL T_LoadData
Reply With Quote #9

Quote:
Originally Posted by ShD3luxe View Post
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

Last edited by GetRektByNoob; 08-22-2019 at 05:09.
GetRektByNoob is offline
ShD3luxe
Member
Join Date: Aug 2019
Location: Localhost
Old 08-22-2019 , 05:42   Re: Problem MySQL T_LoadData
Reply With Quote #10

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 .
ShD3luxe 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 06:14.


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