我无法使插入忽略查询
I can not make insert ignore query
如何使用 knex 进行 insert ignore
查询?
我试过了:
knex.raw(knex('music').insert({title: service[i].Title, author: service[i].PrimaryArtistName, album: service[i].TypeAlbumName, lyrics: service[i].Lyrics}).toString().replace("insert", 'insert ignore'));
但是没用
更新:
如果我添加
它的工作
.then(function (res) {
console.log("res", res);// or another code
});
但是方法对吗?
正在进行拉取请求以在 knex API 中正式支持此功能。我希望它为下一个版本做好准备 https://github.com/tgriesser/knex/pull/2197.
您永远不应使用 .toString()
来构建查询,因为它可能很容易在您的代码中留下 sql 注入漏洞,尤其是在插入时。
一开始它什么也没做,因为你从来没有触发查询。
目前你可以像这样实现同样更安全的方式:
const query = knex('music')
.insert({
title: service[i].Title,
author: service[i].PrimaryArtistName,
album: service[i].TypeAlbumName,
lyrics: service[i].Lyrics
}).toSQL();
const sql = query.sql.replace("insert", 'insert ignore');
knex.raw(sql, query.bindings).then(() => console.log('insert is ready'));
如何使用 knex 进行 insert ignore
查询?
我试过了:
knex.raw(knex('music').insert({title: service[i].Title, author: service[i].PrimaryArtistName, album: service[i].TypeAlbumName, lyrics: service[i].Lyrics}).toString().replace("insert", 'insert ignore'));
但是没用
更新: 如果我添加
它的工作.then(function (res) {
console.log("res", res);// or another code
});
但是方法对吗?
正在进行拉取请求以在 knex API 中正式支持此功能。我希望它为下一个版本做好准备 https://github.com/tgriesser/knex/pull/2197.
您永远不应使用 .toString()
来构建查询,因为它可能很容易在您的代码中留下 sql 注入漏洞,尤其是在插入时。
一开始它什么也没做,因为你从来没有触发查询。
目前你可以像这样实现同样更安全的方式:
const query = knex('music')
.insert({
title: service[i].Title,
author: service[i].PrimaryArtistName,
album: service[i].TypeAlbumName,
lyrics: service[i].Lyrics
}).toSQL();
const sql = query.sql.replace("insert", 'insert ignore');
knex.raw(sql, query.bindings).then(() => console.log('insert is ready'));