Dario M.
Dario M.

Reputation: 425

sqlite not saving french accents like à as $agrave

When i will save into my database "C'est tout à fait juste.", sqlite will remove the & from &agrave, becouse it can't maybe handle utf-8.

var sql += "INSERT INTO Test(TestId,Text) Values(1, "C'est tout à fait juste.");

I tried for every insert to replace the $agrave with à, but I think there will be a better solution.

Is there a better way to solve this?

Thanks for any help...

UPDATE

This is my code to create my sqlite database:

  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();
}

And with this code I insert the data

     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();
    }

In the debug mode I can see, that the data comes in the right way like "C'est tout à fait juste." and after insert when i read out this data it comes like this: "C'est toutagrave; fait juste."

Upvotes: 0

Views: 265

Answers (1)

Dario M.
Dario M.

Reputation: 425

For me this solution worked:

var elemfolderName = document.createElement('textarea');
elemfolderName.innerHTML = "C'est tout à fait juste.";
var  folderName = elemfolderName.value;

And then I save folderName to sqlite3 db...

Upvotes: 0

Related Questions