Safari 5:Javascript sqlite“插入事务”似乎没有正确地看到作用域内的变量
简而言之,我将编程作为一种爱好。我正在尝试将数据插入到 Safari 5 中的 SQLite 数据库中。
我并不担心注入攻击,因为使用自制扩展程序,这仅供我用来从我访问的某些网站收集数据。
我的事务在循环的每次迭代中插入一行,其中包含来自“DOM 更改事件”的数据(最多 20 次左右的迭代),但没有看到正确传递给它的变量。调用“console.log(variable)”表明它们确实包含正确的数据 - 但 INSERT 不会在每个记录中插入正确的(不同的)数据,除了第一个正确递增的 var(通过我自己的增量机制,该机制位于与其他行为不当的变量的范围相同)。
我读到过“工人”的重要性,但对他们一无所知。
注意 Windows 7 Pro
非常感谢您的指点 - 谢谢。
enter code here: HandleDOM_Change = function() {
for (p=0; p<snip.length; p++) {
title = snip[p].getElementsByClassName('title')[0].firstChild.textContent;
value = snip[p].getElementsByClassName('value')[0].firstChild.textContent;
lang = snip[p].getElementsByClassName('lang')[0].textContent;
if (hP[p] != lang) {
++count;
id = count;
db.transaction(function (tx) {
console.log("events:" +events+ " title:" +title+ " value/p+1/lang: " +value+ ", " +(p+1)+ ", " +lang);
tx.executeSql('INSERT INTO foo (id, text, time, name) VALUES (?, ?, ?, ?)', [id, title, value, lang]);
},myTransactionErrorCallback,myTransactionSuccessCallback);
hP[p] = lang;
}
}
++events;
}
//全局变量:events INTEGER、snip ARRAY、count INTEGER 请原谅愚蠢的语法问题 - 我必须删除一些废话以使其可读 - 但本质仍然是控制台日志报告变量包含我想要的内容,但“tx.executeSql”说不。
注意:为了找到问题,已从插入的值中删除了所有约束 - 甚至在声明中也没有坚持使用主键(这在全局级别较早发生)。另外为了澄清,“id”正确递增并在数据库中记录为这样,但其他三个插入了不变的值 - 与日志报告相反(我已经意识到日志不输出 id,但它确实清洁之前 - 老实说)。
非常感谢。
in a nut shell I program as a hobby. I am trying to INSERT data into an SQLite database in Safari 5.
I am not worried about injection attacks because using a home baked extension, this is only for my use to collect data from some sites that I visit.
My tranaction INSERTs a row per iteration of a loop which contains data from a 'DOM change event' for up to 20 or so iterations) doesn't see the variables passed to it correctly. Calling a 'console.log(variable)' shows that they do contain the correct data - but INSERTs do not insert the correct (different) data per record, except for the first var which increments correctly (via my own increment mechanism which is in the same scope as the other misbehaving vars).
I have read about 'workers' being important but know nothing about them.
N.B. Windows 7 Pro
Any pointers gratefully recieved - thanks.
enter code here: HandleDOM_Change = function() {
for (p=0; p<snip.length; p++) {
title = snip[p].getElementsByClassName('title')[0].firstChild.textContent;
value = snip[p].getElementsByClassName('value')[0].firstChild.textContent;
lang = snip[p].getElementsByClassName('lang')[0].textContent;
if (hP[p] != lang) {
++count;
id = count;
db.transaction(function (tx) {
console.log("events:" +events+ " title:" +title+ " value/p+1/lang: " +value+ ", " +(p+1)+ ", " +lang);
tx.executeSql('INSERT INTO foo (id, text, time, name) VALUES (?, ?, ?, ?)', [id, title, value, lang]);
},myTransactionErrorCallback,myTransactionSuccessCallback);
hP[p] = lang;
}
}
++events;
}
//Global variables: events INTEGER, snip ARRAY, count INTEGER
Please excuse silly syntax issues - I had to remove some chaff to make it readable - however the essence remains in that the console logs report that the vars contain what I want but 'tx.executeSql' says no.
N.B. In an attempt to find the problem, all constraints have been removed from inserted values - not even a primary key is insisted upon in the declaration (which occurs earlier at the global level). Also to clarify, 'id' increments properly and is recorded as such in the db but the other three are inserted with an unchanging value - contrary to the log report (I have realised that the log doesn't output the id, but it did prior to cleansing - honestly).
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我想我已经得到了它,并且在一瞬间令人眼花缭乱。在每次迭代期间重新分配的“常见”变量,例如“标题”,但不是简单的整数(“p”直到一个谜题,但我认为引用与值在这里发挥作用)取其最后一个值,并且因此记录器始终是循环最后一次迭代期间的值。为了解决这个问题,我将这些值简单地重新记录到一个新数组中,这样我就不必担心当前数组长度等,使用“push(..)”和“pop(..)”那个数组。我希望现在说我可以继续前进还为时过早。感谢您的查看和任何刮擦。
Think I've got it and in a blinding flash. The 'common' vars such as 'title' that are re-assigned during each iteration, but are not simple integers ('p' is till a puzzle but I think reference versus value comes into play here) are taken at their last value and so it is always the value during the last iteration of the loop that is recorder. To solve, I made a simple re-recording of those values into a new array and so that I didn't have to worry about current array length etc, used 'push(..)' and 'pop(..)' on that array. I hope it is not too soon to say I can move on. Thanks for looking and any scratching.