2017-02-01 14 views
0

Nodejsおよびsocket.ioを使用してmsqlデータベースの変更を通知できる通知アプリケーションを構築しようとしています。Node.jsリアルタイム通知アプリケーションのMysqlデータベースにデータが挿入されない

しかし、mydataはデータベースに挿入されていません。私のdb.jsファイルとsocketDemo.sqlを添付してください。

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 @[email protected]@CHARACTER_SET_CLIENT */; 
/*!40101 SET @[email protected]@CHARACTER_SET_RESULTS */; 
/*!40101 SET @[email protected]@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 [email protected]_CHARACTER_SET_CLIENT */; 
/*!40101 SET [email protected]_CHARACTER_SET_RESULTS */; 
/*!40101 SET [email protected]_COLLATION_CONNECTION */; 

データベース・スキーマ: - 私のデータベース名はsocket.io

db.jsファイルです

enter image description here

助けてください。

答えて

1

私はまずSELECT UserName FROM User WHERE UserName = userを使いたいのですが、SELECTの代わりにuserという変数を使用するだけです。あなたは、このようなクエリを実行していて、関数に渡された変数を使用したい場合は

詳細は何ですか、あなたは.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クエリの?マーク(配列内のこれらの変数の順序を忘れないように注意してください)。

関連する問題