在JavaScript中浏览SQL对象

时间:2016-04-28 17:30:33

标签: javascript mysql arrays node.js

我正在使用以下代码:

//Handles every game and their attributes
function handleGames(){
    sql.query('SELECT id FROM games', function (err, rows){
        if(err){
            console.log(String(err).error.bgWhite);
        } else {
            for(var i = 0; i < rows.length; i++) {
            /** ["id"]
                ["starttime"]
                ["status"]
                ["ct_name"]
                ["ct_avatar"]
                ["ct_steamid"]
                ["ct_bet"]
                ["t_name"]
                ["t_avatar"]
                ["t_steamid"]
                ["t_bet"]           **/

                var timeNow = new Date().getTime();
                //console.log(String(timeNow).info.bgWhite);

                //Game Expiration
                //0 = Open | 4 = Expired | 5 = Generic Error

                //Generic error --> Expired
                if(rows[i]["status"] == 5){
                    sql.query('UPDATE games SET status = 4 WHERE id = "' + rows[i]["id"] + '"', function (err, rows){
                        if(err) console.log(String(err).error.bgWhite);
                        console.log(("Updating status for game " + rows[i]["id"] + " to 4").info.bgWhite);
                    });
                }

                //Gamestatus set to expired if game has lasted 5m (300s)
                if(((rows[i]["starttime"] + 300) >= timeNow) && (rows[i]["status"] == 0)){
                    sql.query('UPDATE games SET status = 4 WHERE id = "' + rows[i]["id"] + '"', function (err, rows){
                        if(err) console.log(String(err).error.bgWhite);
                    });
                }
            };
        }
    });
}

使用以下数据:

enter image description here

该程序是为Node编写的,应该会改变任何持续5分钟(300s)的游戏的状态,但是我无法使用键进行导航。 游戏状态未更新。

JSON Version

SQL导出:

-- phpMyAdmin SQL Dump
-- version 4.0.10.7
-- http://www.phpmyadmin.net
--
-- Host: localhost:3306
-- Generation Time: Apr 28, 2016 at 01:25 PM
-- Server version: 5.6.26-cll-lve
-- PHP Version: 5.4.31

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `csgovoid_data`
--

-- --------------------------------------------------------

--
-- Table structure for table `games`
--

CREATE TABLE IF NOT EXISTS `games` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `starttime` int(11) NOT NULL,
  `status` int(1) NOT NULL COMMENT '0 = Open, 1 = In progress, 2 = CT Win, 3 = T Win, 4 = Expired, 5 = Generic error',
  `ct_name` varchar(255) NOT NULL,
  `ct_avatar` longtext NOT NULL,
  `ct_steamid` bigint(20) NOT NULL,
  `ct_bet` float NOT NULL,
  `t_name` varchar(255) NOT NULL,
  `t_avatar` longtext NOT NULL,
  `t_steamid` bigint(20) NOT NULL,
  `t_bet` float NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

--
-- Dumping data for table `games`
--

INSERT INTO `games` (`id`, `starttime`, `status`, `ct_name`, `ct_avatar`, `ct_steamid`, `ct_bet`, `t_name`, `t_avatar`, `t_steamid`, `t_bet`) VALUES
(3, 1461862619, 0, 'Robinlemon > CSGOVoid.net', 'https://steamcdn-a.akamaihd.net/steamcommunity/public/images/avatars/c5/c5ef952e8bd4ff79afcecb9971ec831b21707be7_full.jpg', 76561198065346589, 3, 'Yuuta', 'http://cdn.akamai.steamstatic.com/steamcommunity/public/images/avatars/73/734a259213585f56c15631a56b127018710d2e34_full.jpg', 76561198052096214, 3);

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

1 个答案:

答案 0 :(得分:1)

所以((new Date()).getTime()-starttime)/1000会给你持续时间,以秒为单位 你能在任何在线javascript编辑器中发布代码,如plunker! 你在这?

INSERT INTO `games` (`id`, `starttime`, `status`, `ct_name`, `ct_avatar`, `ct_steamid`, `ct_bet`, `t_name`, `t_avatar`, `t_steamid`, `t_bet`) VALUES 
(3, 1461862619, 0, 'Robinlemon > CSGOVoid.net', 'steamcdn-a.akamaihd.net/…', 76561198065346589, 3, 'Yuuta', 'cdn.akamai.steamstatic.com/…', 76561198052096214, 3);

这是你的转储sql! 这是插入一个id(带AUTO_INCREMENT)3的实体!! 所以对于下一个插入,AUTO_INCREMENT将是4 !!!

如果你想改变你删除表中的行和重置AUTO_INCREMENT计数器

ALTER TABLE tablename AUTO_INCREMENT = 1