Raised This Month: $ Target: $400
 0% 

Need some SQL help!


Post New Thread Reply   
 
Thread Tools Display Modes
KaLoIaN
Senior Member
Join Date: Feb 2013
Old 06-24-2016 , 09:28   Re: Need some SQL help!
Reply With Quote #11

Somebody, please?
KaLoIaN is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 06-28-2016 , 18:27   Re: Need some SQL help!
Reply With Quote #12

Here's the basic framework.
PHP Code:
#include <amxmodx>
#include <sqlx>

new const Version[] = "0.1";

#define MAX_PLAYERS 32

//You can/should clean this up with an enumerator and a single array.
new g_iLevelMAX_PLAYERS ];
new 
g_iExpMAX_PLAYERS ];
new 
g_iResetMAX_PLAYERS ];
new 
g_iTicketMAX_PLAYERS ];
new 
g_iEscapeMAX_PLAYERS ];
new 
g_iHealthMAX_PLAYERS ];
new 
g_iArmorMAX_PLAYERS ];
new 
g_iDamageMAX_PLAYERS ];
new 
g_iCashMAX_PLAYERS ];

new const 
QUERY_CreateTable[] = "CREATE TABLE IF NOT EXISTS tblPlayerData (SteamID VARCHAR(34) PRIMARY KEY, iLevel INTEGER, iExp INTEGER, iReset INTEGER, iTicket INTEGER, iEscape INTEGER, iHealth INTEGER, iArmor INTEGER, iDamage INTEGER, iCash INTEGER);";
new const 
QUERY_Connected[] = "SELECT iLevel, iExp, iReset, iTicket, iEscape, iHealth, iArmor, iDamage, iCash FROM tblPlayerData WHERE SteamID='%s';";
new const 
QUERY_Disconnected_New[] = "INSERT INTO tblPlayerData (iLevel, iExp, iReset, iTicket, iEscape, iHealth, iArmor, iDamage, iCash, SteamID) VALUES (%d,%d,%d,%d,%d,%d,%d,%d,%d,'%s');";
new const 
QUERY_Disconnected_Return[] = "UPDATE tblPlayerData SET iLevel=%d, iExp=%d, iReset=%d, iTicket=%d, iEscape=%d, iHealth=%d, iArmor=%d, iDamage=%d, iCash=%d WHERE SteamID='%s';";

new 
Handle:g_SQLTuple g_szQueryCache1024 ] , g_szSteamIDMAX_PLAYERS ][ 34 ] , bool:g_bReturningPlayerMAX_PLAYERS ];

public 
plugin_init() 
{
    
register_plugin"SQL Example" Version "bugsy" );
    
    
SQL_SetAffinity"sqlite" );
    
g_SQLTuple SQL_MakeDbTuple"" "" "" "YourDatabase" );
    
    new 
Handle:SqlConnection ErrorCode;
    
SqlConnection SQL_Connectg_SQLTuple ErrorCode g_szQueryCache charsmaxg_szQueryCache ) );
    
    if( 
SqlConnection == Empty_Handle )
        
set_fail_stateg_szQueryCache );
    
    
RunQuerySqlConnection QUERY_CreateTable );
}

public 
plugin_end() 
{
    
SQL_FreeHandleg_SQLTuple );
}

public 
client_authorizedid )
{
    new 
Data]; Data] = id;
    
    if ( !
is_user_botid ) )
    {
        
g_bReturningPlayerid ] = false;
        
        
get_user_authidid g_szSteamIDid ] , charsmaxg_szSteamID[] ) );
        
        
formatexg_szQueryCachecharsmaxg_szQueryCache ) , QUERY_Connected g_szSteamIDid ] );
        
SQL_ThreadQueryg_SQLTuple "SelectHandle" g_szQueryCache Data sizeofData ) );
    }
}

public 
client_disconnectid )
{
    new 
Data]; Data] = id;
    
    if ( !
is_user_botid ) )
    {
        
//Set random values before at disconnect to confirm data is saving
        
g_iLevelid ] = 37;
        
g_iTicketid ] = 22;
        
        
get_user_authidid g_szSteamIDid ] , charsmaxg_szSteamID[] ) );
        
formatexg_szQueryCachecharsmaxg_szQueryCache ) , g_bReturningPlayerid ] ? QUERY_Disconnected_Return QUERY_Disconnected_New g_iLevelid ],g_iExpid ],g_iResetid ],g_iTicketid ],g_iEscapeid ],g_iHealthid ],g_iArmorid ],g_iDamageid ],g_iCashid ]  , g_szSteamIDid ] );
        
        new 
Handle:SqlConnection ErrorCode;
        
SqlConnection SQL_Connectg_SQLTuple ErrorCode g_szQueryCache charsmaxg_szQueryCache ) );
        
        if( 
SqlConnection == Empty_Handle )
            
set_fail_stateg_szQueryCache );
            
        
        
RunQuerySqlConnection g_szQueryCache );
    }
}

public 
RunQueryHandle:SQLConnection , const szQuery[] )
{
    new 
Handle:Query SQL_PrepareQuerySQLConnection szQuery );
    
    if( !
SQL_ExecuteQuery ) )
    {
        
SQL_QueryErrorQuery g_szQueryCache 100 );
        
set_fail_stateg_szQueryCache );
    }
}

public 
SelectHandleFailState Handle:Query Error[] , Errcode Data[] , DataSize )
{
    if( 
FailState == TQUERY_CONNECT_FAILED )
    {
        return 
set_fail_state("Could not connect to SQL database." );
    }
    else if( 
FailState == TQUERY_QUERY_FAILED )
    {
        
log_amx"Query error: %s" Error );
        return 
set_fail_stateg_szQueryCache );
    }
    
    if( 
Errcode )
        return 
log_amx"Error on query: %s" Error );

    new 
id Data];
    
    if ( 
SQL_NumResultsQuery ) )
    {
        
g_bReturningPlayerid ] = true;
        
        
g_iLevelid ] = SQL_ReadResultQuery );
        
g_iExpid ]  = SQL_ReadResultQuery );
        
g_iResetid ] = SQL_ReadResultQuery );
        
g_iTicketid ] = SQL_ReadResultQuery );
        
g_iEscapeid ] = SQL_ReadResultQuery );
        
g_iHealthid ] = SQL_ReadResultQuery );
        
g_iArmorid ] = SQL_ReadResultQuery );
        
g_iDamageid ] = SQL_ReadResultQuery );
        
g_iCashid ] = SQL_ReadResultQuery );
        
        
//Output the 2 items that dummy data was set to @ disconnect. This confirms data successfully saved\loaded via SQL.
        //Output in server console: Loaded 37 22 for player id 2
        
server_print"Loaded %d %d for player id %d" g_iLevelid ] , g_iTicketid ] , id );
    }
    
    return 
PLUGIN_CONTINUE;

__________________

Last edited by Bugsy; 06-28-2016 at 18:29.
Bugsy is offline
KaLoIaN
Senior Member
Join Date: Feb 2013
Old 06-30-2016 , 14:00   Re: Need some SQL help!
Reply With Quote #13

Quote:
Originally Posted by Bugsy View Post
Here's the basic framework.
PHP Code:
#include <amxmodx>
#include <sqlx>

new const Version[] = "0.1";

#define MAX_PLAYERS 32

//You can/should clean this up with an enumerator and a single array.
new g_iLevelMAX_PLAYERS ];
new 
g_iExpMAX_PLAYERS ];
new 
g_iResetMAX_PLAYERS ];
new 
g_iTicketMAX_PLAYERS ];
new 
g_iEscapeMAX_PLAYERS ];
new 
g_iHealthMAX_PLAYERS ];
new 
g_iArmorMAX_PLAYERS ];
new 
g_iDamageMAX_PLAYERS ];
new 
g_iCashMAX_PLAYERS ];

new const 
QUERY_CreateTable[] = "CREATE TABLE IF NOT EXISTS tblPlayerData (SteamID VARCHAR(34) PRIMARY KEY, iLevel INTEGER, iExp INTEGER, iReset INTEGER, iTicket INTEGER, iEscape INTEGER, iHealth INTEGER, iArmor INTEGER, iDamage INTEGER, iCash INTEGER);";
new const 
QUERY_Connected[] = "SELECT iLevel, iExp, iReset, iTicket, iEscape, iHealth, iArmor, iDamage, iCash FROM tblPlayerData WHERE SteamID='%s';";
new const 
QUERY_Disconnected_New[] = "INSERT INTO tblPlayerData (iLevel, iExp, iReset, iTicket, iEscape, iHealth, iArmor, iDamage, iCash, SteamID) VALUES (%d,%d,%d,%d,%d,%d,%d,%d,%d,'%s');";
new const 
QUERY_Disconnected_Return[] = "UPDATE tblPlayerData SET iLevel=%d, iExp=%d, iReset=%d, iTicket=%d, iEscape=%d, iHealth=%d, iArmor=%d, iDamage=%d, iCash=%d WHERE SteamID='%s';";

new 
Handle:g_SQLTuple g_szQueryCache1024 ] , g_szSteamIDMAX_PLAYERS ][ 34 ] , bool:g_bReturningPlayerMAX_PLAYERS ];

public 
plugin_init() 
{
    
register_plugin"SQL Example" Version "bugsy" );
    
    
SQL_SetAffinity"sqlite" );
    
g_SQLTuple SQL_MakeDbTuple"" "" "" "YourDatabase" );
    
    new 
Handle:SqlConnection ErrorCode;
    
SqlConnection SQL_Connectg_SQLTuple ErrorCode g_szQueryCache charsmaxg_szQueryCache ) );
    
    if( 
SqlConnection == Empty_Handle )
        
set_fail_stateg_szQueryCache );
    
    
RunQuerySqlConnection QUERY_CreateTable );
}

public 
plugin_end() 
{
    
SQL_FreeHandleg_SQLTuple );
}

public 
client_authorizedid )
{
    new 
Data]; Data] = id;
    
    if ( !
is_user_botid ) )
    {
        
g_bReturningPlayerid ] = false;
        
        
get_user_authidid g_szSteamIDid ] , charsmaxg_szSteamID[] ) );
        
        
formatexg_szQueryCachecharsmaxg_szQueryCache ) , QUERY_Connected g_szSteamIDid ] );
        
SQL_ThreadQueryg_SQLTuple "SelectHandle" g_szQueryCache Data sizeofData ) );
    }
}

public 
client_disconnectid )
{
    new 
Data]; Data] = id;
    
    if ( !
is_user_botid ) )
    {
        
//Set random values before at disconnect to confirm data is saving
        
g_iLevelid ] = 37;
        
g_iTicketid ] = 22;
        
        
get_user_authidid g_szSteamIDid ] , charsmaxg_szSteamID[] ) );
        
formatexg_szQueryCachecharsmaxg_szQueryCache ) , g_bReturningPlayerid ] ? QUERY_Disconnected_Return QUERY_Disconnected_New g_iLevelid ],g_iExpid ],g_iResetid ],g_iTicketid ],g_iEscapeid ],g_iHealthid ],g_iArmorid ],g_iDamageid ],g_iCashid ]  , g_szSteamIDid ] );
        
        new 
Handle:SqlConnection ErrorCode;
        
SqlConnection SQL_Connectg_SQLTuple ErrorCode g_szQueryCache charsmaxg_szQueryCache ) );
        
        if( 
SqlConnection == Empty_Handle )
            
set_fail_stateg_szQueryCache );
            
        
        
RunQuerySqlConnection g_szQueryCache );
    }
}

public 
RunQueryHandle:SQLConnection , const szQuery[] )
{
    new 
Handle:Query SQL_PrepareQuerySQLConnection szQuery );
    
    if( !
SQL_ExecuteQuery ) )
    {
        
SQL_QueryErrorQuery g_szQueryCache 100 );
        
set_fail_stateg_szQueryCache );
    }
}

public 
SelectHandleFailState Handle:Query Error[] , Errcode Data[] , DataSize )
{
    if( 
FailState == TQUERY_CONNECT_FAILED )
    {
        return 
set_fail_state("Could not connect to SQL database." );
    }
    else if( 
FailState == TQUERY_QUERY_FAILED )
    {
        
log_amx"Query error: %s" Error );
        return 
set_fail_stateg_szQueryCache );
    }
    
    if( 
Errcode )
        return 
log_amx"Error on query: %s" Error );

    new 
id Data];
    
    if ( 
SQL_NumResultsQuery ) )
    {
        
g_bReturningPlayerid ] = true;
        
        
g_iLevelid ] = SQL_ReadResultQuery );
        
g_iExpid ]  = SQL_ReadResultQuery );
        
g_iResetid ] = SQL_ReadResultQuery );
        
g_iTicketid ] = SQL_ReadResultQuery );
        
g_iEscapeid ] = SQL_ReadResultQuery );
        
g_iHealthid ] = SQL_ReadResultQuery );
        
g_iArmorid ] = SQL_ReadResultQuery );
        
g_iDamageid ] = SQL_ReadResultQuery );
        
g_iCashid ] = SQL_ReadResultQuery );
        
        
//Output the 2 items that dummy data was set to @ disconnect. This confirms data successfully saved\loaded via SQL.
        //Output in server console: Loaded 37 22 for player id 2
        
server_print"Loaded %d %d for player id %d" g_iLevelid ] , g_iTicketid ] , id );
    }
    
    return 
PLUGIN_CONTINUE;


Thank you very very much! I will try the code and leave feedback! Again thanks!
Btw I registered the variables the same way in my plugin

Last edited by KaLoIaN; 06-30-2016 at 14:00.
KaLoIaN is offline
KaLoIaN
Senior Member
Join Date: Feb 2013
Old 07-05-2016 , 09:51   Re: Need some SQL help!
Reply With Quote #14

There are no errors but there is something super strange.
At the database there is completely nothing (no columns, no tables and saved data).
When I join the server it sets my g_iLevel to 0 and g_iTicket to 0 value - and thats okay.
But when I retry the server g_iLevel is 37 and g_iTicket is 22 ?

And still the database is completely empty but I think the plugin connects because there are no errors.
I used the @Bugsy's SQL Example.

Last edited by KaLoIaN; 07-05-2016 at 09:52.
KaLoIaN is offline
wickedd
Veteran Member
Join Date: Nov 2009
Old 07-05-2016 , 10:21   Re: Need some SQL help!
Reply With Quote #15

Show us what you tried because Bugsy code worked for me.
__________________
Just buy the fucking game!!!!
I hate No-Steamers and lazy ass people.
wickedd is offline
KaLoIaN
Senior Member
Join Date: Feb 2013
Old 07-05-2016 , 10:23   Re: Need some SQL help!
Reply With Quote #16

The code is 100% the same as Bugsy just I added my DB.

I added it this way:

g_SQLTuple = SQL_MakeDbTuple( "host" , "user" , "pass" , "dbname" );

host is the ip
user is the username with all rights on db
pass is the password for the user
dbname is the database name
KaLoIaN is offline
wickedd
Veteran Member
Join Date: Nov 2009
Old 07-05-2016 , 10:30   Re: Need some SQL help!
Reply With Quote #17

Well I tested it and it worked for me. Did it create the table?
__________________
Just buy the fucking game!!!!
I hate No-Steamers and lazy ass people.
wickedd is offline
KaLoIaN
Senior Member
Join Date: Feb 2013
Old 07-05-2016 , 12:11   Re: Need some SQL help!
Reply With Quote #18

No the plugin does not touches anything in the database. Not even create tables. Nothing..
KaLoIaN is offline
wickedd
Veteran Member
Join Date: Nov 2009
Old 07-05-2016 , 12:23   Re: Need some SQL help!
Reply With Quote #19

Well you're doing something wrong. I suggest you check your database info.
PHP Code:
g_SQLTuple SQL_MakeDbTuple"host" "user" "pass" "dbname" 
Edit: Also make sure your database allows remote connection.
__________________
Just buy the fucking game!!!!
I hate No-Steamers and lazy ass people.

Last edited by wickedd; 07-05-2016 at 12:25.
wickedd is offline
KaLoIaN
Senior Member
Join Date: Feb 2013
Old 07-07-2016 , 08:11   Re: Need some SQL help!
Reply With Quote #20

I tried to make it save the data on the name..
Not worked, got hundred errors lol ....

P.S.
I am not non steamer LOL .....
KaLoIaN is offline
Reply



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 11:49.


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