gpt4 book ai didi

javascript - 无法使用 Node.js 向 mysql 插入数据

转载 作者:行者123 更新时间:2023-11-29 21:30:49 25 4
gpt4 key购买 nike

我正在尝试插入从 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 ****************");
}

});
}

});

});
});

我的插入查询语法有问题。如有任何建议,我们将不胜感激。

最佳答案

使用这个:

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
});

关于javascript - 无法使用 Node.js 向 mysql 插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35283633/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com