AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Reading ini file through sql table (https://forums.alliedmods.net/showthread.php?t=159157)

bibu 06-13-2011 12:20

Reading ini file through sql table
 
I am using this plugin here:

http://forums.alliedmods.net/showthread.php?p=236886

Now it gets the lines by getting the config's dir and reading the lines. Can someone show me a way how to do that with SQL?

Exolent[jNr] 06-13-2011 13:13

Re: Reading ini file through sql table
 
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);



bibu 06-14-2011 17:47

Re: Reading ini file through sql table
 
I am using your "sql_load" function. But where can I enter the advertisements?

[IMG]http://img96.**************/img96/4548/sqltd.jpg[/IMG]

Exolent[jNr] 06-14-2011 20:20

Re: Reading ini file through sql table
 
I don't know what kind of database viewer you're using.

If you have the .ini file set up, you can use the function ini_to_sql().

bibu 06-15-2011 15:58

Re: Reading ini file through sql table
 
Mhmm, still doesn't show any message at all.

Exolent[jNr] 06-15-2011 22:59

Re: Reading ini file through sql table
 
Remember that ini_to_sql() doesn't load them into the plugin, only transfers from the .ini file to the database.

Something quick you could do one time would be:
Code:
load_sql(); // creates table, loads no messages ini_to_sql(); // puts messages from .ini into table load_sql(); // loads messages from table

bibu 06-16-2011 12:40

Re: Reading ini file through sql table
 
Quote:

Originally Posted by Exolent[jNr] (Post 1489141)
Remember that ini_to_sql() doesn't load them into the plugin, only transfers from the .ini file to the database.

Something quick you could do one time would be:
Code:
load_sql(); // creates table, loads no messages ini_to_sql(); // puts messages from .ini into table load_sql(); // loads messages from table

I know, I've got the "ini" file already in my configs dir. And which one does only create table and doesn't load messages?

Exolent[jNr] 06-16-2011 13:09

Re: Reading ini file through sql table
 
I mean you would do them in that order.

The first time is creating the table, but not loading any messages because the table is empty.
The second function puts all the messages in to the table.
The third line loads the messages from the table.

bibu 06-16-2011 15:04

Re: Reading ini file through sql table
 
So you mean this:

After the "sql_load" function, call "ini_to_sql" and in there again "sql_load", right?

ARES[ro] 06-16-2011 16:14

Re: Reading ini file through sql table
 
What Exolent means, do that only once. (do not do it more then once), it may add double the entries if you do it twice...


All times are GMT -4. The time now is 23:26.

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