我在这里有这行代码,显示正在创建的数据库表。我在运行项目时遇到了两个问题。首先是抛出错误:没有这样的表格患者当我按下按钮时单击保存按钮。第二个是函数_populate。它没有填充列表视图。是因为首先,程序没有创建数据库/表吗?它可能有什么问题?
var database = window.openDatabase("nurse", "1.0", "Nurse DB", 1000000);
document.addEventListener('deviceready', onDeviceReady, false);
function onDeviceReady() {
console.log("Ready.");
database.transaction(_tableCreate, onSuccess, onError);
database.transaction(_populate, onSuccess, onError);
init();
$('#_interventions').on('pageshow', $showInterventions);
}
function onSuccess(i) {
console.log("Yay! " + i.code + ': ' + i.message);
}
function onError(err) {
console.log("Oh dear. " + err);
}
function _tableCreate(tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS "category"("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL CHECK("id">=0));');
tx.executeSql('CREATE TABLE IF NOT EXISTS "diagnoses"("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL CHECK("id">=0), "name" VARCHAR(45) NOT NULL, "category" INTEGER NOT NULL CHECK("category">=0), CONSTRAINT "FKdiag_category" FOREIGN KEY("category") REFERENCES "category"("id"));');
tx.executeSql('CREATE INDEX IF NOT EXISTS "diagnoses.FKdiag_category_idx" ON "diagnoses"("category");');
tx.executeSql('CREATE TABLE IF NOT EXISTS "interventions"("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL CHECK("id">=0), "action" TEXT NOT NULL, "priority" TEXT NOT NULL CHECK("priority" IN(\'1\',\'2\',\'3\',\'4\',\'5\')), "category" INTEGER NOT NULL CHECK("category">=0), CONSTRAINT "FKintervention_category" FOREIGN KEY("category") REFERENCES "category"("id"));');
tx.executeSql('CREATE INDEX IF NOT EXISTS "interventions.FKintervention_category_idx" ON "interventions"("category");');
tx.executeSql('CREATE TABLE IF NOT EXISTS "patients"("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL CHECK("id">=0), "name" VARCHAR(45) NOT NULL, "age" INTEGER NOT NULL, "intervention" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, "subjective" VARCHAR(100) DEFAULT NULL, "objective" VARCHAR(100) DEFAULT NULL, "assessment" VARCHAR(100) DEFAULT NULL, "planning" VARCHAR(100) DEFAULT NULL, "evaluation" VARCHAR(100) DEFAULT NULL);');
tx.executeSql('CREATE TABLE IF NOT EXISTS "analysis"("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL CHECK("id">=0), "patient" INTEGER NOT NULL CHECK("patient">=0), "interventions" VARCHAR(200) DEFAULT NULL, CONSTRAINT "FKanalysis_patient" FOREIGN KEY("patient") REFERENCES "patients"("id"));');
tx.executeSql('CREATE INDEX IF NOT EXISTS "analysis.FKanalysis_patient_idx" ON "analysis"("patient");');
}
function _populate(tx) {
tx.executeSql('SELECT COUNT(*) AS count FROM category', [], function(tx, results) {
var count = results.rows.item(0).count;
if (count > 0) {
console.log("Found existing records.");
console.log(count + " items.");
return;
} else {
console.log("Starting with fresh installation.");
$.ajax({
async: false,
url: "file:///android_asset/www/dev/data.sql",
type: 'GET'
}).done(function(data) {
console.log('Data found!');
var sql = data.split("@@@@@");
for (var i = 0; i < sql.length; i++) {
if (sql[i] !== '') {
// console.log("Executing: " + sql[i]);
tx.executeSql(sql[i]);
}
}
console.log('Finished recording data.');
}).fail(onError);
}
}, onError);
}