Database 如何根据以前的结果组织WebOS SQL代码?
我想做一个简单的WebOS Mojo应用程序,将日期添加到数据库中,但在此之前,它需要通过询问数据库来检查一些条件。考虑到WebOS中访问数据库的异步模式,我想知道编写此类代码最有效、最短的方法是什么 例如,我需要确保新日期不在数据库中。然后我需要得到最接近的日期,这样我就可以以天为单位计算差异,如果差异太小,就会抛出一个错误。然后我需要插入新的日期,然后计算平均值Database 如何根据以前的结果组织WebOS SQL代码?,database,webos,Database,Webos,我想做一个简单的WebOS Mojo应用程序,将日期添加到数据库中,但在此之前,它需要通过询问数据库来检查一些条件。考虑到WebOS中访问数据库的异步模式,我想知道编写此类代码最有效、最短的方法是什么 例如,我需要确保新日期不在数据库中。然后我需要得到最接近的日期,这样我就可以以天为单位计算差异,如果差异太小,就会抛出一个错误。然后我需要插入新的日期,然后计算平均值 以同步方式访问数据库很容易做到这一点,但我不太喜欢在执行的不同sql语句的多个成功处理程序中编写部分代码。有更优雅的解决方案吗?您
以同步方式访问数据库很容易做到这一点,但我不太喜欢在执行的不同sql语句的多个成功处理程序中编写部分代码。有更优雅的解决方案吗?您可以使用HTML 5关系数据库函数的内联回调:
function createProject(project, onSuccess) {
if (project.projectId)
throw new StorageError("project already exists");
if (project.path)
throw new StorageError("project already has a path");
project.projectId = ++Tracker.maxLocalId * 4096;
project.path = calcNextProjectPath();
project.normalize();
Tracker.db.transaction(
function (transaction) {
transaction.executeSql(
"INSERT OR ROLLBACK INTO item (dbId, path, hasChildren, kind, summaryText, place, work, responsible, responsibleClass) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
[project.projectId, project.path, project.hasChildren, project.kind, project.title, project.place, project.work, project.responsible, project.responsibleClass],
function (transaction, resultSet) {
Mojo.Log.info("DB: inserted new project item", resultSet.insertId, project.title);
transaction.executeSql(
"INSERT OR ROLLBACK INTO project (projectId, accountId, title, backendKind, backendStatus, backendLastChanged, lastSuccessfulDown) \
VALUES (?, ?, ?, ?, ?, ?, ?)",
[project.projectId, project.accountId, project.title, project.backendKind, project.backendStatus, project.backendLastChanged, project.lastSuccessfulDown],
function (transaction, resultSet) {
Warn.logInfo("created new project", "projectId=" + resultSet.insertId, project.title);
if (onSuccess)
onSuccess();
},
function (transaction, sqlError) {
Warn.bannerError("create", $L("Quit and reset your phone."), "project row insertion failed: ", sqlError.message, sqlError.code, project);
return true; // abort whole transaction
}
);
},
function (transaction, sqlError) { // failure of insert project item
if (sqlError.code === 1 && sqlError.message === "constraint failed" && Mojo.appInfo.id !== "com.outlinetracker.outlinetracker") {
upgradeAlert(true);
} else {
Warn.bannerError("create", $L("Quit and reset your phone."), "project item insertion failed: ", sqlError.message, sqlError.code);
}
return true; // abort whole transaction
}
);
}, // end transaction function
function (sqlError) { // seems to only be called for exceptions in callbacks
Warn.logError($L("Quit and reset your phone."), "transaction 'insert project' failed", sqlError.message, sqlError.code);
}
); // end transaction call
}您可以使用HTML 5关系数据库函数的内联回调:
function createProject(project, onSuccess) {
if (project.projectId)
throw new StorageError("project already exists");
if (project.path)
throw new StorageError("project already has a path");
project.projectId = ++Tracker.maxLocalId * 4096;
project.path = calcNextProjectPath();
project.normalize();
Tracker.db.transaction(
function (transaction) {
transaction.executeSql(
"INSERT OR ROLLBACK INTO item (dbId, path, hasChildren, kind, summaryText, place, work, responsible, responsibleClass) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
[project.projectId, project.path, project.hasChildren, project.kind, project.title, project.place, project.work, project.responsible, project.responsibleClass],
function (transaction, resultSet) {
Mojo.Log.info("DB: inserted new project item", resultSet.insertId, project.title);
transaction.executeSql(
"INSERT OR ROLLBACK INTO project (projectId, accountId, title, backendKind, backendStatus, backendLastChanged, lastSuccessfulDown) \
VALUES (?, ?, ?, ?, ?, ?, ?)",
[project.projectId, project.accountId, project.title, project.backendKind, project.backendStatus, project.backendLastChanged, project.lastSuccessfulDown],
function (transaction, resultSet) {
Warn.logInfo("created new project", "projectId=" + resultSet.insertId, project.title);
if (onSuccess)
onSuccess();
},
function (transaction, sqlError) {
Warn.bannerError("create", $L("Quit and reset your phone."), "project row insertion failed: ", sqlError.message, sqlError.code, project);
return true; // abort whole transaction
}
);
},
function (transaction, sqlError) { // failure of insert project item
if (sqlError.code === 1 && sqlError.message === "constraint failed" && Mojo.appInfo.id !== "com.outlinetracker.outlinetracker") {
upgradeAlert(true);
} else {
Warn.bannerError("create", $L("Quit and reset your phone."), "project item insertion failed: ", sqlError.message, sqlError.code);
}
return true; // abort whole transaction
}
);
}, // end transaction function
function (sqlError) { // seems to only be called for exceptions in callbacks
Warn.logError($L("Quit and reset your phone."), "transaction 'insert project' failed", sqlError.message, sqlError.code);
}
); // end transaction call
}是的,看起来这是唯一可行的方法。我想不出别的什么了。我真的不喜欢太多的缩进级别,但我必须习惯:)是的,看起来这是唯一可行的方法。我想不出别的什么了。我真的不喜欢太多的缩进级别,但我必须习惯:)