我试图插入从TMDB api访问的电影数据。我能够访问数据库并能够插入数据。但是Values( )
中的变量将作为值插入到表中。
以下是我使用的代码:
tmdb.search('movie', { query: searchQuery, language: 'en' }, function (err, results) {
//Informations on first ID
tmdb.infos('movie', results.results[0].id, { language: 'en' }, function (err, results) {
console.log("################ RESULTS FROM API ################");
console.log(results);
console.log("*********** ENDDD ************");
console.log("Movie Name: ", results.title);
var title = results.title;
console.log("imdb id :", results.imdb_id);
var movie_id = results.imdb_id;
console.log("Tagline :" , results.tagline);
console.log("Poster :", results.poster_path);
var pic = results.poster_path;
console.log("Rating : " , results.vote_average);
var rating = results.vote_average;
console.log("Genres :" , results.genres);
console.log("Original language :" , results.original_language);
console.log("Overview :" , results.overview);
var runtime = 100;
var starring = "myself";
var director = "myself";
var genres = "Action";
var studio = "Fox";
var year_released = 2016;
var copies = 10;
var price = 19;
var sqlStatement = " SELECT * FROM movies_info where movie_id='res.id'";
db.connection.query(sqlStatement, function (err, rows) {
if (rows.length == 0) {
var insert = "INSERT INTO movies_info (movie_id,title, runtime, rating,starring,director,genres, studio,year_released,copies,price,poster) VALUES('movie_id','title', runtime, rating,'starring','director','genres', 'studio',year_released,copies,price,'pic')";
db.connection.query(insert, function (err, rows) {
if (err) {
console.log("!!!!!!!!!!!!!!Error Inserting data!!!!!!!!!!!!! " , err);
console.log("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
}
else {
console.log("************ INSERTED *****************");
console.log(rows);
console.log("************* END INSERT ****************");
}
});
}
});
});
});
我的插入查询语法有问题。任何建议将不胜感激。
答案 0 :(得分:0)
使用此:
var movie = {
movie_id: movie_id,
title: title,
runtime: runtime,
rating: rating,
starring: starring,
director: director,
genres: genres,
studio: studio,
year_released: year_released,
copies: copies,
price: price,
poster: pic
}
db.connection.query('INSERT INTO movies_info SET ?', movie, function(err, result) {
// handle result success or fail here
});