我在数据库中插入文本字段值。实际上当用户使用 (,"",'') 逗号单码、双码时我的应用程序崩溃了。我认为我们不能在数据库中添加这些字符?你能告诉我如何删除这个字符吗?我应该只允许不会使插入查询崩溃的这个字符。我在phonegap中使用sqlite。
我发现了这个,但这不是一个好的解决方案,因为数据库中允许有一些字符
$(document).on("keyup", ".caseName_h", function () {
if (/[^\w]/g.test($(this).val())) {
$(this).val($(this).val().replace(/[^\w]/g, ""));
PG_alert('Special characters not allowed!');
}
});
function insertData() {
db.transaction(createTable, errorCB, afterSuccessTableCreation);
}
//create table and insert some record
function createTable(tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS CaseTable (id INTEGER PRIMARY KEY AUTOINCREMENT, CaseName VARCHAR(100) unique NOT NULL ,CaseDate INTEGER ,TextArea VARCHAR(200) NOT NULL)');
tx.executeSql('INSERT OR IGNORE INTO CaseTable(CaseName,CaseDate,TextArea) VALUES ("' + $('.caseName_h').val() + '", "' + $('.caseDate_h').val() + '","' + $('.caseTextArea_h').val() + '")');
}
//function will be called when an error occurred
function errorCB(err) {
navigator.notification.alert("Error processing SQL: " + err.code);
}
//function will be called when process succeed
function afterSuccessTableCreation() {
console.log("success!");
db.transaction(getallTableData, errorCB);
}
//select all from SoccerPlayer
function getallTableData(tx) {
// tx.executeSql("DROP TABLE IF EXISTS a");
tx.executeSql('SELECT * FROM CaseTable', [], querySuccess, errorCB);
}
/*function querySuccess(){
for (var i = 0; i < len; i++) {
alert(result.rows.item(0).CaseName)
db.transaction(function (tx) {
tx.executeSql('SELECT * FROM "'+result.rows.item(0).CaseName+'"', [],
// tx.executeSql('SELECT EXISTS(SELECT * FROM b)', [],
function(tx, results) {
t=results.rows.length;
alert(t)
});
});
}
}*/
function querySuccess(tx, result) {
var len = result.rows.length;
var countDoument = 0
$('#folderData').empty();
for (var i = 0; i < len; i++) {
currentTableName = result.rows.item(i).CaseName;
countElements(currentTableName, function (i) {
return function (result_count) {
countDoument = result_count; // here it count value
$('#folderData').append(
'<li class="caseRowClick" id="' + result.rows.item(i).id + '" data-rel="popup" data-position-to="window">' + '<a href="#">' + '<img src="img/Blue-Folder.png">' + '<h2>' + result.rows.item(i).CaseName + '</h2>' + '<p>' + result.rows.item(i).TextArea + '</p>' + '<p>' + result.rows.item(i).CaseDate + '</p>' + '<span class="ui-li-count">' + countDoument + '</span>' + '</a>' +
'<span class="ctrl togg"><fieldset data-role="controlgroup" data-type="horizontal" data-mini="true" ><button class="edit button_design">Edit</button><button class="del button_design">Delete</button></fieldset><span>' + '</li>');
$('#folderData').listview('refresh');
};
}(i));
}
}
tx.executeSql('INSERT OR IGNORE INTO CaseTable(CaseName,CaseDate,TextArea) VALUES ("' + $('.caseName_h').val() + '", "' + $('.caseDate_h').val() + '","' + $('.caseTextArea_h').val() + '")');