我创建了一个函数,它将从我的数据库中返回字符串“path”。SQLite查询中的javascript for-loop
function getAudio(mid, cb) {
//mid is an array containing the id to some multimedia files.
for(i=0; i < mid.length; i++) {
db.transaction(function(tx) {
tx.executeSql('SELECT * FROM Multimedia WHERE Mid=' + mid[i] + ' AND Type=' + 2, [], function(tx, results) {
//type=2 is audio. (type=1 is picture file etc) There is only one audiofile in the array.
if(results.rows.length > 0) {
path = results.rows.item(0).Path;
cb(path);
}
}, errorCB);
}, errorCBQuery);
}//end for
}//end getAudio()
当我删除了for循环,查询是全成,当for循环是存在的, errorCBQuery或errorCB被调用。
任何想法如何解决这个问题?谢谢:)
大声笑,这只是一个错字:p 已经有很多删除和添加代码的最后一个小时。 我的坏:) – TorK 2012-03-26 11:40:44
@ T.J.Crowder好的,谢谢你通知我。 *编辑* :) – TorK 2012-03-26 11:57:15
你应该使用参数化的sql而不是使用字符串连接,以保护自己免受SQL注入。鉴于你通过的数组参数,我想它会看起来像'tx.executeSql('SELECT * FROM Multimedia WHERE Mid =?AND Type =?',[mid [i],2]' – hugomg 2012-03-26 13:09:41