Domanda

OK, here's a new problem for me. As I once mentioned in one of my earlier questions, I am now using PtokaX and scripting some bots in it. What I need now to do is update my table completely, and interchange the values of two particular columns. I am storing the mainchat counts of all users in a MySQL table named chatstats(currently on MyISAM, but thinking of changing the table to InnoDB).

The table has many rows(nearly 1000) and it keeps increasing almost daily. What I want to do is interchange values of two columns every week(and month; as explained later) and set one of the column values to zero.

The create statement of my table is this:

CREATE TABLE `chatstat` (
`id` BIGINT(10) UNSIGNED NOT NULL AUTO_INCREMENT, `username` VARCHAR(25) NOT NULL,
`totalcount` BIGINT(20) UNSIGNED NOT NULL DEFAULT '0', `thismonth` BIGINT(20) UNSIGNED NOT NULL DEFAULT '0',
`thisweek` INT(10) UNSIGNED NOT NULL DEFAULT '0', `lastweek` INT(10) UNSIGNED NOT NULL DEFAULT '0',
`lastmonth` INT(10) UNSIGNED NOT NULL DEFAULT '0', `switched` TINYINT(3) UNSIGNED NOT NULL DEFAULT '0',
PRIMARY KEY (`id`), UNIQUE INDEX `id` (`id`), UNIQUE INDEX `username` (`username`)
) COLLATE='utf8_general_ci' ENGINE=MyISAM ROW_FORMAT=DEFAULT;

Now, the column names are self-explanatory. I created the column switched to check if the user's count has been interchanged for that month or not(by checking if its 1 or 0) on every Sunday. Every sunday, I want to change the values of thisweek with lastweek and that too, only once. Currently, my script is as follows(in LUA and PtokaX variables)

function UpdateUserStat( con, user )
        sNick = ConvertNick( user.sNick )
        cur = assert( con:execute( string.format( [[SELECT * FROM chatstat WHERE username = '%s']], sNick ) ) )
        row = cur:fetch( {}, "a" )
        cur:close()
        if row then
                res = assert( con:execute( string.format( [[UPDATE chatstat SET totalcount = totalcount + 1 WHERE id='%d' ]], row.id ) ) )
        else
                res = assert( con:execute( string.format( [[ INSERT INTO chatstat ( username, totalcount, thismonth, thisweek ) VALUES ( '%s', 1, 1, 1 ) ]], sNick ) ) )
        end
        cur = assert( con:execute( string.format( [[SELECT * FROM chatstat WHERE username = '%s']], sNick ) ) )
        row = cur:fetch( {}, "a" )
        cur:close()
        UpdateDateStat( con, row )
        if os.date( "%w" ) ~= 0 then
                if row.switched == 1 then
                        res = assert( con:execute( string.format( [[UPDATE chatstat SET switched = 0 WHERE id='%d' ]], row.id ) ) )
                end
                res = assert( con:execute( string.format( [[SELECT * FROM datestat WHERE field='today']] ) ) )
                tRow = res:fetch( {}, "a" )
                if tRow.value ~= os.date( "%w" ) then
                        ChangeWeekDay = assert( con:execute( string.format( [[UPDATE datestat SET value='%d' WHERE field='today']], os.date( "%w" ) ) ) )
                end
                res:close()
        end
end

The function datestat is to just keep the log of chats with dates(how many messages per day etc.). Any help will be appreciated. The current UpdateUserStat function is doing nothing regarding the change of values(as I checked it yesterday).

P.S. If anything else is needed, and I can manage it, I would be more than happy to supply it. :)

È stato utile?

Soluzione

update chatstat set lastweek = thisweek, thisweek = 0, switched = NOW() 
where WEEK(switched) <> WEEK(NOW()) and (DAYOFWEEK(NOW()) = 1);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top