好的,我正在尝试让一些代码正常运行而且我目前的设置没有太多运气,但不知道如何改变它...
我得到的错误是: Uncaught InvalidStateError:尝试使用不可用或不再可用的对象。
所以我猜想数据连接在添加数据时是不可用的?
任何帮助,指点超过欢迎!
var WebSqlStore = function(successCallback, errorCallback) {
var eventsURL='http://www.url.com/api/eventsWEBSQL.php';
this.initializeDatabase = function(successCallback, errorCallback) {
var self = this;
this.db = window.openDatabase("AADB", "1.0", "App DB", 200000);
this.db.transaction(
function(tx) {
self.createEvents(tx);
self.addEvents(tx);
},
function(error) {
console.log('Transaction error: ' + error);
if (errorCallback) errorCallback();
},
function() {
console.log('Transaction success');
if (successCallback) successCallback();
}
)
}
this.createEvents = function(tx) {
tx.executeSql('DROP TABLE IF EXISTS events');
var sql = "CREATE TABLE IF NOT EXISTS events ( " +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"userid INTEGER, " +
"name VARCHAR(255), " +
"subname VARCHAR(255), " +
"startdate DATETIME, " +
"duration VARCHAR(255), " +
"location VARCHAR(100), " +
"address VARCHAR(255), " +
"city VARCHAR(255), " +
"county VARCHAR(255), " +
"postcode VARCHAR(15), " +
"telephone VARCHAR(20), " +
"modules VARCHAR(255), " +
"description TEXT, " +
"feedback TEXT, " +
"archive INTEGER, " +
"live INTEGER" +
")";
tx.executeSql(sql, null,
function() {
console.log('Create table success');
},
function(tx, error) {
alert('Create table error: ' + error.message);
});
}
this.addEvents = function(tx) {
$.ajax({
type: "GET",
url: eventsURL,
dataType: 'jsonp',
data: { LN: 0 },
success:function (data) {
var events = data
var l = events.length;
//console.log("Events length: "+l);
var sql = "INSERT OR REPLACE INTO events " +
"(id, userid, name, subname, startdate, duration, location, address, city, county, postcode, telephone, modules, description, feedback, archive, live) " + //16
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
var e;
for (var i = 0; i < l; i++) {
e = events[i];
tx.executeSql(sql, [e.id, e.userid, e.name, e.subname, e.startdate, e.duration, e.location, e.address, e.city, e.county, e.postcode, e.telephone, e.modules, e.description, e.feedback, e.archive, e.live],
function() {
console.log('INSERT success');
},
function(tx, error) {
alert('INSERT error: ' + error.message);
}
);
}
},
error:function(error) {
app.showAlert("Internet access is required to get new items, Will continue using local data ", "Events");
console.log(error);
}
});
} //End addEvents
this.initializeDatabase(successCallback, errorCallback);
}
答案 0 :(得分:0)
好的,所以对于像我这样的人并找到自己的方式......解决方案是添加另一个连接:
var WebSqlStore = function(successCallback, errorCallback) {
var eventsURL='http://www.url.com/api/eventsWEBSQL.php';
var dbSize = 5 * 1024 * 1024; // 5MB
var webdb = openDatabase("AADB", "1.0", "App DB", dbSize);
this.initializeDatabase = function(successCallback, errorCallback) {
var self = this;
this.db = webdb;
this.db.transaction(
function(tx) {
self.createEvents(tx);
self.addEvents(tx);
},
function(error) {
console.log('Transaction error: ' + error);
if (errorCallback) errorCallback();
},
function() {
console.log('Transaction success');
if (successCallback) successCallback();
}
)
}
this.createEvents = function(tx) {
tx.executeSql('DROP TABLE IF EXISTS events');
var sql = "CREATE TABLE IF NOT EXISTS events ( " +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"userid INTEGER, " +
"name VARCHAR(255), " +
"subname VARCHAR(255), " +
"startdate DATETIME, " +
"duration VARCHAR(255), " +
"location VARCHAR(100), " +
"address VARCHAR(255), " +
"city VARCHAR(255), " +
"county VARCHAR(255), " +
"postcode VARCHAR(15), " +
"telephone VARCHAR(20), " +
"modules VARCHAR(255), " +
"description TEXT, " +
"feedback TEXT, " +
"archive INTEGER, " +
"live INTEGER" +
")";
tx.executeSql(sql, null,
function() {
console.log('Create table success');
},
function(tx, error) {
alert('Create table error: ' + error.message);
});
}
this.addEvents = function(tx) {
$.ajax({
type: "GET",
url: eventsURL,
dataType: 'jsonp',
data: { LN: 0 },
success:this.insertEvents,
error:function(error) {
app.showAlert("Internet access is required to get new items, Will continue using local data ", "Events");
console.log(error);
}
});
} //End addEvents
this.insertEvents = function(data) {
this.db = webdb;
this.db.transaction(
function(tx) {
var events = data
var l = events.length;
console.log("Events length: "+l);
var sql = "INSERT OR REPLACE INTO events " +
"(id, userid, name, subname, startdate, duration, location, address, city, county, postcode, telephone, modules, description, feedback, archive, live) " + //16
"VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
var e;
for (var i = 0; i < l; i++) {
e = events[i];
tx.executeSql(sql, [e.id, e.userid, e.name, e.subname, e.startdate, e.duration, e.location, e.address, e.city, e.county, e.postcode, e.telephone, e.modules, e.description, e.feedback, e.archive, e.live],
function() {
console.log('INSERT success');
},
function(tx, error) {
alert('INSERT error: ' + error.message);
}
);
}
}
);
}
}