我不熟悉使用,并且在我的应用程序中一直使用cordova-sqlite-storage plugin的修改版本。创建并打开数据库并成功创建表,但是当我插入一项时,它会重复多次,而不是只输入一次。所有重复项都有唯一的ID。我尝试了多种修改方式,但遇到了麻烦。我不知道为什么它在插入时增加了这么多。感谢您的帮助!
这是我的代码(如果有点长的话,请道歉):
var myDB;
var vetDrugsId = 0;
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
myDB = window.sqlitePlugin.openDatabase({ name: "my.db", iosDatabaseLocation: 'Documents' });
CreateDB();
RefreshDrugs();
}
// Create Registration table in Sql Lite DB.
function CreateDB() {
myDB.transaction(function (transaction) {
transaction.executeSql('CREATE TABLE IF NOT EXISTS vetDrugs (Id integer primary key, title text, desc text, conc integer, dose integer, minimum integer, maximum integer)', [],
function (tx, result) {
console.log("Table created successfully");
},
function (error) {
console.log("Error occurred while creating the table.");
});
});
}
//Insert New Details (into SQLite Database)
$(document).on('click touchstart','#insert',function () {
var title=$("#title").val();
var desc=$("#desc").val();
var conc=parseInt($("#conc").val());
var dose=parseInt($("#dose").val());
var minimum=parseInt($("#minimum").val());
var maximum=parseInt($("#maximum").val());
myDB.transaction(function (transaction) {
if (vetDrugsId > 0) {
myDB.transaction(function (transaction) {
var executeQuery = "UPDATE vetDrugs SET title=?, desc=? ,conc=?, dose=?, minimum=?, maximum=? WHERE Id=?";
transaction.executeSql(executeQuery, [title,desc,conc,dose,minimum,maximum,vetDrugsId],
//On Success
function (tx, result) {
RefreshDrugs();
ClearControl();
$('#title,#desc,#conc,#dose,#minimum,#maximum').removeClass('error-border');
toastUpdate.open();
},
function (error) {
alert('Details not updated');
});
});
}
else {
var executeQuery = "INSERT INTO vetDrugs (title, desc, conc, dose, minimum, maximum) VALUES (?,?,?,?,?,?)";
transaction.executeSql(executeQuery, [title,desc,conc,dose,minimum,maximum]
, function (tx, result) {
RefreshDrugs();
ClearControl();
$('#title,#desc,#conc,#dose,#minimum,#maximum').removeClass('error-border');
//alert('Inserted successfully');
toastAdd.open();
},
function (error) {
//alert('Details not Inserted');
toastAddError.open();
});
}
});
});
// Load Data in Table from SQLite Database.
function RefreshDrugs() {
$("#TableData").html("");
myDB.transaction(function (transaction) {
transaction.executeSql('SELECT * FROM vetDrugs', [], function (tx, results) {
var len = results.rows.length, i;
$("#rowCount").html(len);
for (i = 0; i < len; i++) {
$('#TableData').append('<div class="table-row"><div class="column index hidden-print checkbox-cell"><a class="link actions-open" href="#" data-actions=".my-actions'+results.rows.item(i).Id+'"><i class="far fa-cog fa-fw fa-2x"></i></a><div class="actions-modal my-actions'+results.rows.item(i).Id+'"><div class="list"><ul><li><a href="#" class="edit list-button actions-close" id="edit_'+results.rows.item(i).Id+'"><i class="far fa-pencil fa-fw fa-lg"></i> Edit Drug</a></li><li><a id="delete_'+results.rows.item(i).Id+'" class="delete list-button actions-close" href="#" data-id="'+results.rows.item(i).Id+'"><i class="far fa-trash-alt fa-fw fa-lg"></i> Delete Drug</a></li><li><a class="btnCancel list-button actions-close" href="#"><i class="far fa-ban fa-fw fa-lg"></i> Cancel</a></li></ul></div></div></div><div class="wrapper attributes"><div class="wrapper drugName-concentration-condition-range"><div class="wrapper drugName-concentration"><div class="column drugName"><span class="drugTitle">'+results.rows.item(i).title+'</span> <p class="subtitle">'+results.rows.item(i).conc+' mg/ml</p> </div></div><div class="wrapper condition-range"><div class="column range">'+results.rows.item(i).desc+'</div></div></div></div><div class="wrapper results"><div class="column result"><input class="form-control form-control-sm drug" data-appendDose=" mg" data-appendVol=" ml" data-mg="#'+results.rows.item(i).Id+'mg" data-ml="#'+results.rows.item(i).Id+'ml" name="'+results.rows.item(i).Id+'name" type="number" data-conc="'+results.rows.item(i).conc+'" data-min="'+results.rows.item(i).minimum+'" data-max="'+results.rows.item(i).maximum+'" data-step="0.1" data-postfix="mg/kg" data-decimal="2" data-value="'+results.rows.item(i).dose+'" value="'+results.rows.item(i).dose+'" ><span class="error-message"><i class="fas fa-exclamation-triangle fa-sm fa-fw hidden-print" data-fa-transform="up-2"></i> Above Range</span></div><div class="column result"><span class="dosage bg-color-blue color-white" id="'+results.rows.item(i).Id+'mg" ></span></div><div class="column result"><span class="vol bg-color-green color-white" id="'+results.rows.item(i).Id+'ml" ></span></div></div></div>');
}
}, null);
});
}
// Get and Set Current Selected data.
$(document).on('click touchstart', '.edit', function () {
app.accordion.open("#panel10");
var delString = this.id;
var splitId = delString.split("_");
var curId = splitId[1];
//alert(curId);
vetDrugsId = parseInt(curId);
if (vetDrugsId > 0) {
myDB.transaction(function (transaction) {
transaction.executeSql('SELECT * FROM vetDrugs where Id=?', [vetDrugsId], function (tx, results) {
var len = results.rows.length, i;
if (len > 0) {
$("#title").val(results.rows.item(0).title);
$("#desc").val(results.rows.item(0).desc);
$("#conc").val(results.rows.item(0).conc);
$("#dose").val(results.rows.item(0).dose);
$("#minimum").val(results.rows.item(0).minimum);
$("#maximum").val(results.rows.item(0).maximum);
$('#title,#desc,#conc,#dose,#minimum,#maximum').addClass('error-border');
}
else {
$('#title,#desc,#conc,#dose,#minimum,#maximum').removeClass('error-border');
ClearControl();
}
}, null);
});
}
});
// Cancel the current Operation.
$(".btnCancel").on('click touchstart',function () {
ClearControl();
});
// Clear Control
function ClearControl() {
vetDrugsId = 0;
$("#title").val('');$("#desc").val(''); $("#conc").val(''); $("#dose").val('');
$("#minimum").val(''); $("#maximum").val('');
$('#title,#desc,#conc,#dose,#minimum,#maximum').removeClass('error-border');
}
//Delete Selected Registration data from SQLite Database.
$(document).on('click touchstart', '.delete', function () {
if (confirm("Do you want to delete Drug?")) {
var delString = this.id;
var splitId = delString.split("_");
var curId = splitId[1];
//alert(curId);
id = parseInt(curId);
//var ID = this.id;
//var id = $(this).attr('data-id');
myDB.transaction(function (transaction) {
transaction.executeSql("DELETE FROM vetDrugs where Id=?", [id],
//Success
function (tx, result) {
alert('Deleted successfully');
ClearControl();
RefreshDrugs();
},
//Error
function (error) { alert('Data not deleted.'); });
});
}
});
$(document).on('click touchstart','#DropTable',function () {
if (confirm("Are you sure you want to delete everything?")) {
myDB.transaction(function(transaction) {
//var executeQuery = "DROP TABLE IF EXISTS myDrugs";
//transaction.executeSql(executeQuery, [],
transaction.executeSql("DELETE FROM vetDrugs", [],
function(tx, result) {alert('Data deleted successfully.');
RefreshDrugs();
},
function(error){alert('Error occurred while deleting the table.');}
);
});
}
});