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

SQL


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
gubka
Veteran Member
Join Date: Jan 2012
Location: Russia
Old 03-15-2016 , 19:06   SQL
Reply With Quote #1

Hi everyone, i have a problem i live in halls, and i do not have any databases and internet is terrible . i cannot connect to any free mysql bases. Who can look on my code and say, how i can improve it, and it is look good ? because i didn't work with mysql ever

Some of the code
PHP Code:
public void OnPluginStart(/*void*/)
{
    if(
SQL_CheckConfig(AMMOPACK_DATABASE_STRING))
    {
        
// Initialize some chars
        
char sError[BIG_LINE_LENGTH];
        
char sRequest[BIG_LINE_LENGTH];
        
char sDriver[SMALL_LINE_LENGTH]; 
        
        
// Creates an SQL connection from a named configuration
        
DataBase SQL_Connect(AMMOPACK_DATABASE_STRINGfalsesErrorsizeof(sError));

        
// If base doesn't exist or mod can't make connection
        
if(DataBase == NULL
            
SetFailState("[ZBM3] ERROR:[007]     Can't connect to MySQL server (%s)\n\n"sError);
        
        
// Reads the driver of an opened database
        
SQL_ReadDriver(DataBasesDriverSMALL_LINE_LENGTH); 
        
        
// If driver is not a MySQL
        
if(!StrEqual(sDriver"mysql"false))
            
SetFailState("[ZBM3] ERROR:[008]     Driver of connection must to be a MySQL. Edit databases.cfg\n\n");

        
// Block mysql base from other requests
        
SQL_LockDatabase(DataBase);

        
// Create database
        
Format(sRequestBIG_LINE_LENGTH,     "CREATE TABLE IF NOT EXISTS `zp_ammopacks_table`         \
                                            (`id` int(11) NOT NULL auto_increment,                    \
                                            `steam` varchar(31) NOT NULL,                             \
                                            `ammopacks` int(11) NOT NULL,                                 \
                                             PRIMARY KEY  (`id`),                                    \
                                             UNIQUE KEY `steam` (`steam`))"
);                 
        
SQL_FastQuery(DataBasesRequest);        

        
// Unblock mysql base
        
SQL_UnlockDatabase(DataBase);
    } 
    else 
        
SetFailState("[ZBM3] ERROR:[009]     Section %s doesn't found in databases.cfg\n\n"AMMOPACK_DATABASE_STRING);
}

public 
void OnClientPostAdminCheck(int clientIndex)
{
    
gDataID[clientIndex] = -1;

    
GetClientAuthId(clientIndexAuthId_Steam2gSteamID[clientIndex], sizeof(gSteamID));
    
    
// Initialize char
    
char sRequest[BIG_LINE_LENGTH];
    
    
Format(sRequestBIG_LINE_LENGTH"SELECT id, ammopacks FROM zp_ammopacks_table WHERE steam = '%s'"gSteamID[clientIndex]);
    
    
SQL_TQuery(DataBaseMySQL_ClientConnectedsRequestGetClientUserId(clientIndex));
}

public 
void OnClientDisconnect(int clientIndex)
{
    
// Initialize char
    
char sRequest[BIG_LINE_LENGTH];
    
    if( 
gDataIDclientIndex ] == -1)
        
Format(sRequestBIG_LINE_LENGTH"UPDATE zp_ammopacks_table SET ammopacks = %i WHERE steam = '%s'"iAmmoPackgSteamID[clientIndex]);
    else 
        
Format(sRequestsBIG_LINE_LENGTH"UPDATE zp_ammopacks_table SET ammopacks = %i WHERE id = %i"iAmmoPackgDataIDclientIndex ]); 
    
    
SQL_TQuery(DataBaseMySQL_ClientDisconnectedsRequest);
}


public 
void MySQL_ClientConnected(Handle sParentHandle sHandle, const char[] sErrorany clientID)
{
    
// Get real player index from event key
    
int clientIndex GetClientOfUserId(clientID);
 
    
// Make sure the client didn't disconnect while the thread was running
    
if (!clientIndex || gSteamID[clientIndex][0] != 'S')
    {
        return;
    }
    
    
// If invalid query handle
    
if (sHandle == NULL)
    {
        
LogError("[ZBM3] ERROR:[010]     Query load fail: %s"sError);
        return;
    }
    
    
// Client not found, add him into the MySQL Base
    
if (!SQL_GetRowCount(sHandle)) 
    {
        
// Initialize char
        
char sRequest[BIG_LINE_LENGTH];
    
        
// Create request
        
Format(sRequestBIG_LINE_LENGTH"INSERT INTO zp_ammopacks_table(steam) VALUES('%s')"gSteamID[clientIndex]);
        
SQL_FastQuery(DataBasesRequest);
        return;
        
    }
    
    
// Client was found in the table
    
if(SQL_FetchRow(sHandle))
    {
        
gDataID[clientIndex]         = SQL_FetchInt(sHandle0); // Load client ID
        
gAmmoPacks[clientIndex]        = SQL_FetchInt(sHandle1); // Load client ammopacks
    
}
}

public 
void MySQL_ClientDisconnected(Handle sParentHandle sHandle, const char[] sErrorany data)
{
    
// If invalid query handle
    
if (sHandle == NULL)
    {
        
LogError("[ZBM3] ERROR:[011]     Query save fail: %s"sError);
        return;
    }

__________________

Last edited by gubka; 03-15-2016 at 19:58.
gubka is offline
Send a message via ICQ to gubka
xbatista
Veteran Member
Join Date: Mar 2008
Location: Lithuania
Old 03-16-2016 , 04:02   Re: SQL
Reply With Quote #2

Code looks well, there is nothing special to change.

Quote:
because i didn't work with mysql ever
I can evaluate you as for first time. It is 10+/10

You even optimized updating, by id...:
PHP Code:
    if( gDataIDclientIndex ] == -1)
        
Format(sRequestBIG_LINE_LENGTH"UPDATE zp_ammopacks_table SET ammopacks = %i WHERE steam = '%s'"iAmmoPackgSteamID[clientIndex]);
    else 
        
Format(sRequestsBIG_LINE_LENGTH"UPDATE zp_ammopacks_table SET ammopacks = %i WHERE id = %i"iAmmoPackgDataIDclientIndex ]); 
__________________

Last edited by xbatista; 03-16-2016 at 04:10.
xbatista is offline
Send a message via Skype™ to xbatista
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 20:58.


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