vendredi 13 février 2015

Cordova SQLite Uft8 throws error on insert

I have an app written with ionic and cordova that inserts names in a database. The db connection is opened like so:



var db = window.openDatabase('zpassplus', '1.0', 'ZPass+', 1000000);


Then data is fetched from the server via an http, then inserted into a table like so:



$http({
url: settings().api_host + 'contact?token=' + $rootScope.authToken,
method: "GET",
data: {'token': $rootScope.authToken},
headers: {
'Content-Type':'application/x-www-form-urlencoded; charset=utf-8'
}
})
.success(function(data, status) {
if(data.success === true) {
db.transaction(function(tx) {

for(var i in data.data.contact.contactRiders)
{
var sqlString, active, rider = data.data.contact.contactRiders[i];

if(rider.active) {
active = 1;
} else {
active = 0;
}

sqlString = "INSERT OR IGNORE INTO riders (fname,lname,imagedata,active,riderId) VALUES('" + rider.firstName + "','" + rider.lastName + "','" + $rootScope.defaultPortrait +"'," + active + "," + rider.riderId + ")";

console.log("insert into riders: " + sqlString);
tx.executeSql(sqlString);
}

}, function(err) {
//error callback
console.log("Error processing SQL: "+err.message);
}, function(){
// this should be the last thing
console.log("rider insert success");
$location.path('/activity');
$rootScope.$apply();
});
}
})
.error(function(err) {
// ummm ....
});


The problem is this, when a utf8 string is passed as one of the rider properties, the SQL statement fails. Because I have that console.log just before the execute I have the offending SQL statement, it is:



INSERT OR IGNORE INTO riders (fname,lname,imagedata,active,riderId) VALUES('Nñicole','Rock','<base64 endcode image here>',1,8824)


The error it throws is this:



Error processing SQL: near "t": syntax error


I ran this same routine with an empty string for the base64 encoded image string, and got the same error and again ran it with an empty string for the first name, and it succeeded. So I'm quite sure its the ñ that's causing the problem. But I can't duplicate it in the command line or anywhere else. Further, it seems that SQLite should be perfectly comfortable with UTF8. What am I missing?


Aucun commentaire:

Enregistrer un commentaire