当我将保存到我的数据库“C'esttoutàfaitjuste。”时,sqlite将删除&来自& agrave,因为它不能处理utf-8。
var sql + =“INSERT INTO Test(TestId,Text)值(1,”C'esttoutàfaitjuste。“);
我尝试用每个插件用à替换$ agrave,但我认为会有更好的解决方案。
有没有更好的方法来解决这个问题?
感谢您的帮助......
更新
这是我创建sqlite数据库的代码:
function initDb($cordovaSQLite, logService) {
var defcorrect = $.Deferred();
try {
logger = logService;
db = window.sqlitePlugin.openDatabase({ name: "xxx.db", location: 0 }, successCB, errorCB);
$cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS Card(CardId UNIQUEIDENTIFIER PRIMARY KEY, CardSetId UNIQUEIDENTIFIER NOT NULL, FrontText NVARCHAR(4000) NOT NULL, BackText NVARCHAR(4000) NULL, ControlType INT NOT NULL, CardLevel INT NOT NULL, IsDirty bit NOT NULL, ChangedAt INT NOT NULL, Active bit default 'true')").catch(function (err) {
logService.writeAppError("00000000-0000-0000-0000-000000000000", "Could not init Table Card: ErrorNo: " + startGUIDLog + " ErrorMessage: " + err.message);
window.location = "#/error/" + startGUIDLog;
});
$cordovaSQLite.execute(db, "CREATE UNIQUE INDEX IF NOT EXISTS Card_Index on Card (CardId)").catch(function (err) {
logService.writeAppError("00000000-0000-0000-0000-000000000000", "Could not init INDEX Card_Index on Card: ErrorNo: " + startGUIDLog + " ErrorMessage: " + err.message);
window.location = "#/error/" + startGUIDLog;
});
defcorrect.resolve(db);
} catch (err) {
logService.writeAppError("00000000-0000-0000-0000-000000000000", "Could not init Database: ErrorNo: " + startGUIDLog + " ErrorMessage: " + err.message);
window.location = "#/error/" + startGUIDLog;
}
return defcorrect.promise();
}
使用此代码我插入数据
function saveCard(cardList, userId, db) {
var defcorrect = $.Deferred();
truncateTable("Card", db, userId).done(function () {
if (cardList.length !== 0) {
var sql = "";
cardList.forEach(function (card) {
sql += "INSERT INTO Card(CardId,CardSetId,FrontText,BackText,ControlType,CardLevel,IsDirty,ChangedAt,Active) VALUES (" + JSON.stringify(card.CardId) + "," + JSON.stringify(card.CardSetId) + "," + JSON.stringify(Card.FrontText) + "," + JSON.stringify(Card.BackText) + "," + card.ControlType + ", " + card.CardLevel + ",'" + card.IsDirty + "'," + card.ChangedAt + ",'" + card.Active + "');";
});
var successFn = function (count) {
logService.writeAppInfo(userId, "Sync Save Card Successfully imported " + count + " SQL statements to DB");
defcorrect.resolve();
};
var errorFn = function (error) {
logService.writeAppError(userId, "Sync Save Card Error: " + error);
$('#statusOverview').hide();
$('#syncError').show();
};
var progressFn = function (current, total) {
$("#statusText").text("Importiere " + current + "/" + total + " Karten");
};
cordova.plugins.sqlitePorter.importSqlToDb(db, sql, {
successFn: successFn,
errorFn: errorFn,
progressFn: progressFn,
batchInsertSize: 500
});
} else {
defcorrect.resolve();
}
});
return defcorrect.promise();
}
在调试模式中,我可以看到,数据以正确的方式出现,如“C'esttoutàfaitjuste”。插入后,当我读出这些数据时,它就像这样:“C'est toutagrave; fait juste。”
答案 0 :(得分:0)
对我来说,这个解决方案有效:
var elemfolderName = document.createElement('textarea');
elemfolderName.innerHTML = "C'est tout à fait juste.";
var folderName = elemfolderName.value;
然后我将folderName保存到sqlite3 db ...