数据未插入Node.js中的Mysql数据库实时通知应用程序

时间:2017-02-01 17:49:02

标签: mysql node.js database socket.io

我正在尝试构建一个通知应用程序,它可以使用Nodejs和socket.io通知msql数据库中的更改。

但是mydata没有插入数据库中。附加我的db.js文件和socketDemo.sql。我的数据库名称是socket.io

db.js文件: -

    var addComment = function(user,comment,mysql,pool,callback) {
    console.log(user,comment);
    var self = this;
    pool.getConnection(function(err,connection){
        if (err) {
            //connection.release();
            return callback(true,null);
        } else {
            var sqlQuery = "INSERT into UserComment (UserName,UserId,Comment) VALUES ((SELECT UserName FROM User WHERE UserName = user),id,comment)";
           // var inserts =["UserComment","UserId","UserName",
            //                         "Comment","UserId","User","UserName",
              //                        user,user,comment];
            //sqlQuery = mysql.format(sqlQuery,inserts);
            connection.query(sqlQuery,function(err,rows){
                connection.release();
                if (err) {
                    return callback(true,null);
                } else {
                    callback(false,"comment added");
                }
            });
        }
        connection.on('error', function(err) {
            return callback(true,null);
        });
    });
};

module.exports.addComment = addComment;

socketDemo.sql: -

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 utf8mb4 */;

--
-- Database: `socketDemo`
--

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

--
-- Table structure for table `User`
--

CREATE TABLE IF NOT EXISTS `User` (
  `UserId` int(11) NOT NULL,
  `UserName` varchar(25) NOT NULL,
  `Password` varchar(25) NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

--
-- Dumping data for table `User`
--

INSERT INTO `User` (`UserId`, `UserName`, `Password`) VALUES
(1, 'Harshit', 'Harshit');

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

--
-- Table structure for table `UserComment`
--

CREATE TABLE IF NOT EXISTS `UserComment` (
  `UserId` int(11) NOT NULL,
  `UserName` varchar(11) NOT NULL,
  `Comment` text NOT NULL,
  `PostId` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

--
-- Dumping data for table `UserComment`
--

INSERT INTO `UserComment` (`UserId`, `UserName`, `Comment`, `PostId`) VALUES
(1, 'Harshit', '\n          \n          \n          \n          \n      ', 0);


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

--
-- Table structure for table `UserPost`
--

CREATE TABLE IF NOT EXISTS `UserPost` (
  `UserPostId` int(11) NOT NULL,
  `UserPostContent` text NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

--
-- Dumping data for table `UserPost`
--

INSERT INTO `UserPost` (`UserPostId`, `UserPostContent`) VALUES
(1, 'This is test comment.');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `User`
--
ALTER TABLE `User`
  ADD PRIMARY KEY (`UserName`),
  ADD KEY `UserIdIndex` (`UserId`);

--
-- Indexes for table `UserComment`
--
ALTER TABLE `UserComment`
  ADD KEY `UserIdIndexComment` (`UserId`),
  ADD KEY `PostIdIndex` (`PostId`);

--
-- Indexes for table `UserPost`
--
ALTER TABLE `UserPost`
  ADD PRIMARY KEY (`UserPostId`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `User`
--
ALTER TABLE `User`
  MODIFY `UserId` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=2;
--
-- AUTO_INCREMENT for table `UserPost`
--
ALTER TABLE `UserPost`
  MODIFY `UserPostId` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=2;
/*!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 */;

数据库架构: -

enter image description here

请帮忙。

1 个答案:

答案 0 :(得分:1)

我的第一个问题是您为什么要使用SELECT UserName FROM User WHERE UserName = user - 您只需使用user变量而不是SELECT语句。

此外,如果您正在执行此类查询并且想要使用传递给函数的变量,则需要将它们传递给.query方法:

connection.query("INSERT into UserComment (UserName,UserId,Comment) VALUES (?, ?, ?)", [user, id, comment], function(error, results){
    // check error and perform further operations...
});

[user, id, comment]部分用于替换SQL查询中的?标记(记住要维护数组中这些变量的顺序)。