我使用以下代码:
//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);
});
}
};
}
});
}
具有以下数据:
该程序是为 Node 编写的,应该会更改任何持续 5 分钟(300 秒)的游戏的状态,但是我在使用按键导航时遇到了问题。 游戏状态未更新。
<强> 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 */;
最佳答案
so ((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 为 3 的实体(带有 AUTO_INCRMENT)! 因此对于下一次插入,AUTO_INCRMENT 将为 4!!!
如果您想更改,请删除表中的行并重置 AUTO_INCRMENT 计数器
ALTER TABLE tablename AUTO_INCREMENT = 1
关于javascript - 在 JavaScript 中浏览 SQL 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36921649/