AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved SQL Database (https://forums.alliedmods.net/showthread.php?t=293275)

sky_pg 01-27-2017 19:38

SQL Database
 
hi all

i have this code to cleanup old entries in my database
PHP Code:

public CleanDataBase()
{
    new 
curTime get_systime();
    
curTime -= ((30 24) * 3600);
    
    new 
szTemp[512]
    new 
ErrorCode,Handle:SqlConnection SQL_Connect(g_SqlTuple,ErrorCode,g_Error,charsmax(g_Error))
    if(
g_SqlTuple == Empty_Handle)
        
set_fail_state(g_Error)
    
formatex(szTemp,charsmax(szTemp),"DELETE FROM `global_stats` WHERE `timestamp` < '%d';"curTime);    
    
    
SQL_ThreadQuery(g_SqlTuple,"IgnoreHandle",szTemp)
    
SQL_FreeHandle(SqlConnection)


how can i see whole info aboute this entry? (steamid-name) for example
this is my table
PHP Code:

Queries SQL_PrepareQuery(SqlConnection,"CREATE TABLE IF NOT EXISTS global_stats (`steamid-name` varchar(32), xp TEXT(11), level INT(11), `timestamp` INT(10) NOT NULL DEFAULT 0, name varchar(32))"

point of this is i want to log deleted entries

Bugsy 01-27-2017 21:24

Re: SQL Database
 
You would do:
Code:

SELECT * FROM `global_stats` WHERE `timestamp` < '%d';

OR

SELECT Field1, Field2, Field3 FROM `global_stats` WHERE `timestamp` < '%d';

In your query handler (IgnoreHandle) you would then read the results and write to log file.

sky_pg 01-28-2017 06:39

Re: SQL Database
 
Quote:

Originally Posted by Bugsy (Post 2490423)
You would do:
Code:

SELECT * FROM `global_stats` WHERE `timestamp` < '%d';

OR

SELECT Field1, Field2, Field3 FROM `global_stats` WHERE `timestamp` < '%d';

In your query handler (IgnoreHandle) you would then read the results and write to log file.



im really noob at this if u can give me example that would be nice...

PHP Code:

formatex(szTemp,charsmax(szTemp),"SELECT `steamid-name`, `xp`, `level` FROM `global_stats` WHERE `timestamp` < '%d';"curTime


sky_pg 01-28-2017 12:41

Re: SQL Database
 
Quote:

Originally Posted by Bugsy (Post 2490423)
You would do:
Code:

SELECT * FROM `global_stats` WHERE `timestamp` < '%d';

OR

SELECT Field1, Field2, Field3 FROM `global_stats` WHERE `timestamp` < '%d';

In your query handler (IgnoreHandle) you would then read the results and write to log file.

im not sure im i geting it right..

PHP Code:

SQL_ThreadQuery(g_SqlTuple"displayFunction""SELECT * FROM `global_stats` WHERE `timestamp` < '%d';"); 

PHP Code:

public displayFunction(failStateHandle:QueryError[], errorIdData[], dataSizeFloat:queueTime)
{
        
// Only reach here if there is no connection or invalid query
    
if (failState || errorId || Error[0])
                
log_amx("SQL_ThreadQuery Failed! FailState = %d | Error = %s | errorId = %d"failStateErrorerrorId);
 
        else
        {
        if (
SQL_NumRows(Query))
        {
                        
// Stress results
                        
while (SQL_MoreResults(Query))
                        {
                                new 
Name[512];
                                
SQL_ReadResult(Query0Namecharsmax(Name));
            
log_to_file("addons/amxmodx/logs/deleted.log","%s",Name)
                                
SQL_NextRow(Query);
                        }
 
                        
// Rewind from zero
                        
SQL_Rewind(Query);
 
                        
// Stress again
                        
while (SQL_MoreResults(Query))
                        {
                                new 
Name[512];
                                
SQL_ReadResult(Query0Namecharsmax(Name));
 
                                
SQL_NextRow(Query);
 
                                
// Print
                                
server_print(Name);
            
log_to_file("addons/amxmodx/logs/deleted.log","%s",Name)
                        }
                }
 
                
// Get field's name
                
new fieldName[512];
                
SQL_FieldNumToName(Query0fieldNamecharsmax(fieldName));
 
                
server_print(fieldName);
        
log_to_file("addons/amxmodx/logs/deleted.log","%s",fieldName)
        }




result>
PHP Code:

L 01/28/2017 18:36:04steamid-name 


Bugsy 01-28-2017 12:53

Re: SQL Database
 
What you need to do is first call your query to retrieve what will be deleted so they can be written to your log. Once you execute that query, execute the delete query which you do not to do any handling in the query callback...once it executes, it is done and all records are deleted.
PHP Code:

public displayFunctionFailState Handle:Query Error[] , errorId Data[] , dataSize Float:queueTime )
{
    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_stateError );
    }
    else
    {
        if ( 
SQL_NumRowsQuery ) )
        {
            new 
Name64 ] , iXP iLevel;
            
            while ( 
SQL_MoreResultsQuery ) )
            {
                
//SELECT `steamid-name`, `xp`, `level` FROM `global_stats` WHERE `timestamp` < %d;
            
                // 0 = `steamid-name`
                // 1 = `xp`
                // 2 = `level`
            
                
SQL_ReadResultQuery NamecharsmaxName ) );
                
iXP SQL_ReadResultQuery );
                
iLevel SQL_ReadResultQuery );
            
                
log_to_file"addons/amxmodx/logs/deleted.log" "Name=%s XP=%d Level=%d" Name iXP iLevel )
            
                
SQL_NextRowQuery );
            }
        
        }
    }



sky_pg 01-28-2017 13:45

Re: SQL Database
 
Bugsy You are the man! TY

All good now..im switching from nvault to sql


All times are GMT -4. The time now is 20:53.

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