Raised This Month: $ Target: $400
 0% 

Reading ini file through sql table


  
 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
Exolent[jNr]
Veteran Member
Join Date: Feb 2007
Location: Tennessee
Old 06-13-2011 , 13:13   Re: Reading ini file through sql table
Reply With Quote #2

These 2 functions may help:

PHP Code:
public sql_load()
{
    new 
Handle:tuple SQL_MakeStdTuple();
    
    if(
tuple == Empty_Handle)
        return;
    
    new 
errcodeerror[128];
    new 
Handle:db SQL_Connect(tupleerrcodeerrorcharsmax(error));
    
    if(
db == Empty_Handle)
    {
        
log_amx("%s"error);
        return;
    }
    
    new 
Handle:query SQL_PrepareQuery(db,
        
"CREATE TABLE IF NOT EXISTS ads \
        (message VARCHAR(128) NOT NULL, \
        autorespond VARCHAR(32) NOT NULL DEFAULT '', \
        condition_map VARCHAR(32) NOT NULL DEFAULT '', \
        condition_minplayers VARCHAR(32) NOT NULL DEFAULT '', \
        condition_maxplayers VARCHAR(32) NOT NULL DEFAULT '');"
);
    
    if(!
SQL_Execute(query))
    {
        
SQL_QueryError(queryerrorcharsmax(error));
        
log_amx("Error creating table: %s"error);
    }
    
    
SQL_FreeHandle(query);
    
    
query SQL_PrepareQuery(db"SELECT message, autorespond, condition_map, condition_minplayers, condition_maxplayers FROM ads;");
    
    if(!
SQL_Execute(query))
    {
        
SQL_QueryError(queryerrorcharsmax(error));
        
log_amx("Error selecting ads: %s"error);
    }
    else if(
SQL_NumResults(query))
    {
        new 
message[128], said[32], typeicondition[32];
        
        while(
SQL_MoreResults(query))
        {
            
SQL_ReadResult(query0messagecharsmax(message));
            
SQL_ReadResult(query1saidcharsmax(said));
            
            
type said[0] ? SAY_AD NORM_AD;
            
            for(
03i++)
            {
                
SQL_ReadResult(query, (2), conditioncharsmax(condition));
                
                if(!
condition[0])
                    continue;
                
                
setString(CONDtypeconditionadCount[type], i);
            }
            
            
setColor(messagecharsmax(message));
            
            if(
type == SAY_AD)
            {
                
setString(STORESAY_ADsaidadCount[SAY_AD], 0);
                
setString(STORESAY_ADmessageadCount[SAY_AD], 1);
            }
            else
                
setString(STORENORM_ADmessageadCount[NORM_AD]);
            
            
adCount[type]++;
            
            
SQL_NextRow(query);
        }
    }
    
    
SQL_FreeHandle(query);
    
    
SQL_FreeHandle(db);
    
    
//Set a first task, if there are any normal ads
    
if(adCount[NORM_AD] != 0)
        
set_task(random_float(RAND_MINRAND_MAX), "eventTask");
}

public 
ini_to_sql()
{
    new 
Handle:tuple SQL_MakeStdTuple();
    
    if(
tuple == Empty_Handle)
        return;
    
    new 
errcodeerror[128];
    new 
Handle:db SQL_Connect(tupleerrcodeerrorcharsmax(error));
    
    if(
db == Empty_Handle)
    {
        
log_amx("%s"error);
        return;
    }
    
    new 
Handle:query SQL_PrepareQuery(db"DELETE FROM ads;");
    
    if(!
SQL_Execute(query))
    {
        
SQL_QueryError(queryerrorcharsmax(error));
        
log_amx("Error deleting all ads: %s"error);
    }
    
    
SQL_FreeHandle(query);
    
    
//Load the data
    
new filepath[64];
    
get_configsdir(filepath63);
    
format(filepath63"%s/advertisements.ini"filepath);
    
    if(
file_exists(filepath))
    {
        new 
output[512], conditions[128], temp[64], type;
        
        
//Open file
        
new fHandle fopen(filepath"rt");
        
        
//Checks for failure
        
if(!fHandle)
            return;
        
        
//Loop through all lines
        
for(new 0MAXADS && !feof(fHandle); a++)
        {
            
//Get line
            
fgets(fHandleoutput511);
            
            
            
//Work away comments
            
if(output[0] == ';' || !output[0] || output[0] == ' ' || output[0] == 10
            {
                
//Line is not counted
                
a--;
                continue;
            }
            
            
//Reset type
            
type 0;
            
            new 
map[32], minplayers[32], maxplayers[32], said[32];
            
            
//Check if it contains conditions
            
if(output[0] == COND_TKN)
            {
                
//Cut the conditions off the string
                
split(outputconditions127output511DEVIDE_STKN);
                
                
//Determine if its say check or normal ad
                
type output[0] == SAY_TKN 0;
                
                
//Put the conditions in own space
                
for(new 03b++)
                {
                    new 
sort[16], cond[32], numb;
                    
                    
//Remove the % from line 
                    
conditions[0] = ' ';
                    
trim(conditions);
                    
                    
//Get one condition from the line
                    
split(conditionstemp64conditions127COND_STKN);
                    
                    
split(tempsort15cond31" ");
                    
                    if(
equali(sort"map"))
                    {
                        
copy(map31cond);
                    } else if(
equali(sort"min_players"))
                    {
                        
copy(minplayers31cond);
                    } else if(
equali(sort"max_players"))
                    {
                        
copy(maxplayers31cond);
                    } else
                    {
                        continue;
                    }
                    
                    
//Exit if it hasn't got more conditions
                    
if(!conditions[0])
                        break;
                }
            }
            
            if(
type == 0)
                
type output[0] == SAY_TKN 0;
            
            if(
type == SAY_AD)
            {
                
//Remove the @ from line
                
output[0] = ' ';
                
trim(output);
                
                
split(outputsaid31output127DEVIDE_STKN);
            }
            
            
query SQL_PrepareQuery(db,
                
"INSERT INTO ads \
                (message, autorespond, condition_map, condition_minplayers, condition_maxplayers) \
                VALUES \
                (^"
%s^", ^"%s^", ^"%s^", ^"%s^", ^"%s^");",
                
outputsaidmapminplayersmaxplayers);
            
            if(!
SQL_Execute(query))
            {
                
SQL_QueryError(queryoutputcharsmax(output));
                
log_amx("Error inserting ad: %s"output);
            }
            
            
SQL_FreeHandle(query);
        }
        
        
//Close file to prevent lockup
        
fclose(fHandle);    
    }
    
    
SQL_FreeHandle(db);

__________________
No private work or selling mods.
Quote:
Originally Posted by xPaw View Post
I love you exolent!

Last edited by Exolent[jNr]; 06-16-2011 at 20:18.
Exolent[jNr] is offline
 



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 23:26.


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