我正在尝试构建一个通知应用程序,该应用程序可以使用 Nodejs 和 socket.io 通知 msql 数据库中的更改。
但是mydata没有入到数据库中。附加我的数据库.js文件和套接字演示.sql。我的数据库名称是 socket.io
数据库.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;
套接字演示.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 */;
数据库架构:-
在此处输入图像描述
请帮忙。
我的第一个问题是为什么要使用 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 查询中的?
标记(请记住维护数组中这些变量的顺序)。