Raised This Month: $ Target: $400
 0% 

Solved SQL Database


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
sky_pg
Member
Join Date: Apr 2012
Old 01-27-2017 , 19:38   SQL Database
Reply With Quote #1

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

Last edited by sky_pg; 01-28-2017 at 13:44.
sky_pg is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-27-2017 , 21:24   Re: SQL Database
Reply With Quote #2

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.
__________________
Bugsy is offline
sky_pg
Member
Join Date: Apr 2012
Old 01-28-2017 , 06:39   Re: SQL Database
Reply With Quote #3

Quote:
Originally Posted by Bugsy View Post
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 is offline
sky_pg
Member
Join Date: Apr 2012
Old 01-28-2017 , 12:41   Re: SQL Database
Reply With Quote #4

Quote:
Originally Posted by Bugsy View Post
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 

Last edited by sky_pg; 01-28-2017 at 12:41.
sky_pg is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 01-28-2017 , 12:53   Re: SQL Database
Reply With Quote #5

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 );
            }
        
        }
    }

__________________

Last edited by Bugsy; 01-28-2017 at 12:55.
Bugsy is offline
sky_pg
Member
Join Date: Apr 2012
Old 01-28-2017 , 13:45   Re: SQL Database
Reply With Quote #6

Bugsy You are the man! TY

All good now..im switching from nvault to sql
sky_pg is offline
Reply


Thread Tools
Display Modes

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:53.


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