View Single Post
WAR3DM
Senior Member
Join Date: Mar 2016
Old 09-07-2016 , 23:24   Re: [SQLite] Combining Rows like nVault
Reply With Quote #8

Quote:
Originally Posted by Bugsy View Post
There are 40 skills that each player can have and you need to store data for? Can you explain your plugin?
The plugin is a heavily edited UWC3 Next Generation (RPG Mod)



Here is the SQLite Code: (You can ignore everything not related to Skills)
PHP Code:
// Uwc3ng SQLITE functions

#define TOTAL_SQLITE_TABLES 4

new const szTablesSQLite[TOTAL_SQLITE_TABLES][] = 
{
    
"CREATE TABLE `uwc3ng_player` ( `player_id` INTEGER PRIMARY KEY AUTOINCREMENT, `player_steamid` varchar(25) NOT NULL default '', `player_ip` varchar(20) NOT NULL default '', `player_name` varchar(35) NOT NULL default '', `player_xp` int(16) default NULL );",
    
"CREATE TABLE `uwc3ng_skill` ( `player_id` int(8) NOT NULL default '0', `skill_id` int(8) NOT NULL default '0', `skill_level` int(8) default NULL, PRIMARY KEY  (`player_id`,`skill_id`) );",
    
"CREATE TABLE `uwc3ng_attrib` ( `player_id` int(8) NOT NULL default '0', `attrib_id` int(8) NOT NULL default '0', `attrib_level` int(8) NOT NULL default '0', PRIMARY KEY  (`player_id`,`attrib_id`) );",
    
"CREATE TABLE `uwc3ng_resist` ( `player_id` int(8) NOT NULL default '0', `resist_id` int(8) NOT NULL default '0', `resist_level` int(8) NOT NULL default '0', PRIMARY KEY  (`player_id`,`resist_id`) );"
};

new const 
szTableNames[TOTAL_SQLITE_TABLES][] = 
{
    
"uwc3ng_player",
    
"uwc3ng_skill",
    
"uwc3ng_attrib",
    
"uwc3ng_resist"
};

// Initiate the connection to the SQLite database
public SQLITE_Init()
{
    new 
szError[256], iErrNum;

    
// Set up the tuple that will be used for threading
    
g_DBTuple SQL_MakeDbTuple"""""""uwc3ng" );

    
// Attempt to connect
    
g_DBConn SQL_Connectg_DBTupleiErrNumszError255 );

    if ( !
g_DBConn )
    {
        
log_amx"[SQLITE] Database Connection Failed: [%d] %s"iErrNumszError );

        return;
    }


    
server_print"%s SQLite database connection successful"MOD_NAME );

    new 
Handle:query;

    
// Create the default tables if we need to
    
for ( new 0TOTAL_SQLITE_TABLESi++ )
    {
        
query SQL_PrepareQueryg_DBConnszTablesSQLite[i] );

        if ( !
sqlite_TableExistsg_DBConnszTableNames[i] ) )
        {
            if ( !
SQL_Executequery ) )
            {
                
SQLITE_ErrorqueryszTablesSQLite[i], );

                return;
            }
        }

        
SQL_FreeHandlequery );
    }

    
/*
        These probably should be subject to a CVAR
        Lets fine tune the database:
            "synchronous = NORMAL"    - Put back the 2.x behaviour (faster than the defalt
                          for 3.x)
            "synchronous = OFF"    - Way faster, but it might get corrupted data if a
                          server os system crash occurs
            "integrity_check"    - well it's what it says, we do have to check the
                          value it returns since it's important
        PRAGMA commands don't return anything so no need to check the result of the query
    */    

    
query SQL_PrepareQueryg_DBConn"PRAGMA integrity_check" );
    
    if ( !
SQL_Executequery ) )
    {
        
SQLITE_Errorquery"PRAGMA integrity_check");

        return;
    }
    
    
// Get the integrity check value
    
new szIntegrityCheck[64];
    if ( 
SQL_NumResultsquery ) > )
    {
        
SQL_ReadResultquery0szIntegrityCheckcharsmaxszIntegrityCheck ) );
    }

    
// Free the result
    
SQL_FreeHandlequery );

    
// Check to make sure the integrity check passed
    
if ( !equali(szIntegrityCheck"ok") )
    {
        
// Should we disable saving here?
        
log_amx"[SQLITE] SQL Lite integrity check failed, disabling saving XP." );

        
set_pcvar_numCVAR_uwc3ng_save_xp);

        return;
    }
    
    
// Do some synchronous crap
    
new szQuery[128];
    
formatszQuerycharsmaxszQuery ), "PRAGMA synchronous = %d"SQLITE_SYNC_OFF );
    
query SQL_PrepareQueryg_DBConnszQuery );

    if ( !
SQL_Executequery ) )
    {
        
SQLITE_ErrorqueryszQuery);

        return;
    }
    
    
// Free the last handle
    
SQL_FreeHandlequery );
}

// Close the SQLite connection
public SQLITE_Close()
{
    if ( 
g_DBTuple )
    {
        
SQL_FreeHandleg_DBTuple );
    }

    if ( 
g_DBConn )
    {
        
SQL_FreeHandleg_DBConn );
    }
}

public 
SQLITE_FetchUniqueIDid )
{
    
// Make sure our connection is working
    
if ( !SQLITE_Connection_Available() )
    {
        return;
    }

    new 
szKey[66], szKeyName[32];
    
Save_GetKeyidszKeycharsmaxszKey ) );
    
Save_GetKeyNameszKeyNamecharsmaxszKeyName ) );

    new 
szQuery[512];
    
formatszQuerycharsmaxszQuery ), "SELECT `player_id` FROM `uwc3ng_player` WHERE `%s` = '%s';"szKeyNameszKey );
    new 
Handle:query SQL_PrepareQueryg_DBConnszQuery );

    if ( !
SQL_Executequery ) )
    {
        
SQLITE_ErrorqueryszQuery);

        return;
    }
    
    
// If no rows we need to insert!
    
if ( SQL_NumResultsquery ) == )
    {
        
// Free the last handle!
        
SQL_FreeHandlequery );

        
// Insert this player!
        
formatszQuerycharsmaxszQuery ), "INSERT INTO `uwc3ng_player` ( `player_id` , `%s` ) VALUES ( NULL , '%s' );"szKeyNameszKey );
        
query SQL_PrepareQueryg_DBConnszQuery );

        if ( !
SQL_Executequery ) )
        {
            
SQLITE_ErrorqueryszQuery);

            return;
        }

        
p_data[id][PLAYER_UNIQUEID] = SQL_GetInsertIdquery );
    }

    
// They have been here before - store their ID
    
else
    {
        
p_data[id][PLAYER_UNIQUEID] = SQL_ReadResultquery);
    }

    
// Free the last handle!
    
SQL_FreeHandlequery );
}

public 
SQLITE_Saveid )
{
    
// Make sure our connection is working
    
if ( !SQLITE_Connection_Available() )
    {
        return;
    }
    
    
// Get the UniqueID
    
new iUniqueID SAVE_GetUniqueIDid );
    
    new 
szKey[66], szKeyName[32];
    
Save_GetKeyidszKeycharsmaxszKey ) );
    
Save_GetKeyNameszKeyNamecharsmaxszKeyName ) );

    
// Save the user's XP!
    
new szQuery[512];
    
formatexszQuerycharsmaxszQuery ), "REPLACE INTO `uwc3ng_player` ( `player_id` , `%s` , `player_xp` ) VALUES ( '%d', '%s', '%d');"szKeyNameiUniqueIDszKeyp_data[id][PLAYER_XP] );
    new 
Handle:query SQL_PrepareQueryg_DBConnszQuery );

    if ( !
SQL_Executequery ) )
    {
        
SQLITE_ErrorqueryszQuery);

        return;
    }

    
// Now we need to save the skill levels!
    
for ( new iSkillID 0iSkillID MAX_SKILLSiSkillID++ )
    {
        
formatexszQuery511"REPLACE INTO `uwc3ng_skill` ( `player_id` , `skill_id` , `skill_level` ) VALUES ( '%d', '%d', '%d' );"iUniqueIDiSkillIDp_data_skill[id][iSkillID] );
        
query SQL_PrepareQueryg_DBConnszQuery );

        if ( !
SQL_Executequery ) )
        {
            
SQLITE_ErrorqueryszQuery);

            return;
        }
    }
    
    
// Now we need to save the attrib levels!
    
for ( new iAttribID 0iAttribID MAX_ATTRIBSiAttribID++ )
    {
        
formatexszQuery511"REPLACE INTO `uwc3ng_attrib` ( `player_id` , `attrib_id` , `attrib_level` ) VALUES ( '%d', '%d', '%d' );"iUniqueIDiAttribIDp_data_attrib[id][iAttribID] );
        
query SQL_PrepareQueryg_DBConnszQuery );

        if ( !
SQL_Executequery ) )
        {
            
SQLITE_ErrorqueryszQuery);

            return;
        }
    }
    
    
// Now we need to save the resist levels!
    
for ( new iResistID 0iResistID MAX_RESISTiResistID++ )
    {
        
formatexszQuery511"REPLACE INTO `uwc3ng_resist` ( `player_id` , `resist_id` , `resist_level` ) VALUES ( '%d', '%d', '%d' );"iUniqueIDiResistIDp_data_resist[id][iResistID] );
        
query SQL_PrepareQueryg_DBConnszQuery );

        if ( !
SQL_Executequery ) )
        {
            
SQLITE_ErrorqueryszQuery);

            return;
        }
    }
    
    return;
}

public 
SQLITE_GetAllXPid )
{
    
// Make sure our connection is working
    
if ( !SQLITE_Connection_Available() )
    {
        return;
    }

    
// Then we have a problem and cannot retreive the user's XP
    
if ( p_data[id][PLAYER_UNIQUEID] <= )
    {
        
client_printidprint_chat"%s Unable to retreive your XP from the database"MOD_NAME );

        
log_amx"[ERROR] Unable to retreive user's Unique ID" );

        return;
    }

    new 
szQuery[256];
    
format(szQuerycharsmaxszQuery ), "SELECT `player_xp` FROM `uwc3ng_player` WHERE ( `player_id` = '%d' );"p_data[id][PLAYER_UNIQUEID] );
    new 
Handle:query SQL_PrepareQueryg_DBConnszQuery );

    if ( !
SQL_Executequery ) )
    {
        
SQLITE_ErrorqueryszQuery);

        return;
    }

    
// Set last saved XP to 0
    
p_data[id][PLAYER_XP] = 0;

    
// Read the XPs from the table
    
p_data[id][PLAYER_XP] = SQL_ReadResultquery);
        
    
// Free the handle
    
SQL_FreeHandlequery );
    
    
// Select the right Table
    
formatszQuerycharsmaxszQuery ), "SELECT `skill_id`, `skill_level` FROM `uwc3ng_skill` WHERE ( `player_id` = '%d' );"p_data[id][PLAYER_UNIQUEID] );
    
query SQL_PrepareQueryg_DBConnszQuery );

    
// Check if the DB ready
    
if ( !SQL_Executequery ) )
    {
        
SQLITE_Error queryszQuery ,);

        return;
    }
    
    
// While we have a result!
    
new iSkillID 0;
    while ( 
SQL_MoreResultsquery ) )
    {
        
iSkillID SQL_ReadResultquery);
        
p_data_skill[id][iSkillID] = SQL_ReadResultquery);

        
SQL_NextRowquery );
    }
    
    
// Close the connection
    
SQL_FreeHandlequery );
    
    
// Select the right Table
    
formatszQuerycharsmaxszQuery ), "SELECT `attrib_id`, `attrib_level` FROM `uwc3ng_attrib` WHERE ( `player_id` = '%d' );"p_data[id][PLAYER_UNIQUEID] );
    
query SQL_PrepareQueryg_DBConnszQuery );

    
// Check if the DB ready
    
if ( !SQL_Executequery ) )
    {
        
SQLITE_Error queryszQuery);

        return;
    }
    
    
// While we have a result!
    
new iAttribID 0;
    while ( 
SQL_MoreResultsquery ) )
    {
        
iAttribID SQL_ReadResultquery);
        
p_data_attrib[id][iAttribID] = SQL_ReadResultquery);

        
SQL_NextRowquery );
    }
    
    
// Close the connection
    
SQL_FreeHandlequery );
    
    
// Select the right Table
    
formatszQuerycharsmaxszQuery ), "SELECT `resist_id`, `resist_level` FROM `uwc3ng_resist` WHERE ( `player_id` = '%d' );"p_data[id][PLAYER_UNIQUEID] );
    
query SQL_PrepareQueryg_DBConnszQuery );

    
// Check if the DB ready
    
if ( !SQL_Executequery ) )
    {
        
SQLITE_Error queryszQuery);

        return;
    }
    
    
// While we have a result!
    
new iResistID 0;
    while ( 
SQL_MoreResultsquery ) )
    {
        
iResistID SQL_ReadResultquery);
        
p_data_resist[id][iResistID] = SQL_ReadResultquery);

        
SQL_NextRowquery );
    }
    
    
// Close the connection
    
SQL_FreeHandlequery );

    return;
}

// Verifies that the database connection is ok
bool:SQLITE_Connection_Available()
{
    if ( !
g_DBConn )
    {
        return 
false;
    }

    return 
true;
}

// The id should be a unique number, so we know what function called it (useful for debugging)
SQLITE_ErrorHandle:queryszQuery[], id )
{
    new 
szError[256];
    new 
iErrNum SQL_QueryErrorqueryszError255 );

    
log_amx"[SQLITE] Error in querying database, location: %d"id );
    
log_amx"[SQLITE] Message: %s (%d)"szErroriErrNum );
    
log_amx"[SQLITE] Query statement: %s "szQuery );

    
// Free the handle
    
SQL_FreeHandlequery );
}
/* AMXX-Studio Notes - DO NOT MODIFY BELOW HERE
*{\\ rtf1\\ ansi\\ deff0{\\ fonttbl{\\ f0\\ fnil Tahoma;}}\n\\ viewkind4\\ uc1\\ pard\\ lang1031\\ f0\\ fs16 \n\\ par }
*/ 

Last edited by WAR3DM; 09-08-2016 at 01:02.
WAR3DM is offline