user1857654
user1857654

Reputation: 283

Phonegap take photo and insert image (BLOB) into database SQLite

Hey everyone Ive been working on a project to take photos and store them in a database using phonegap/jQuery/html5. The database entry needs to store some geolocation information and the BLOB of the image that is taken by the camera. This is the current method I am using to try to do this and its not working out for me. The BLOB always breaks my insert statement. If I set the smallImage to "1" instead of the image data it works fine. Is there a better way to insert this blob? When I look at the log it looks like the smallImage is cut off.

var cameraLat;
var cameraLong;
var cameraTimestamp;
var destinationType;
var cameraLocationID;

function onGeoSuccess(position) {
    cameraLat = position.coords.latitude;
    console.log(cameraLat);
    cameraLong = position.coords.longitude;
    console.log(cameraLong);
    cameraTimestamp = position.timestamp;
    console.log(cameraTimestamp);
}

function geoFail(position) {
    alert('code: '    + error.code    + '\n' +
            'message: ' + error.message + '\n');
}

function onPhotoDataSuccess(imageData) {                
   // Get image handle 
    navigator.geolocation.getCurrentPosition(onGeoSuccess, geoFail);
    var smallImage = imageData;
    var Latitude = cameraLat;
    var longitude = cameraLong;
    var timestamp = cameraTimestamp;
    var LID = cameraLocationID;
    console.log(Latitude); //working
    console.log(longitude); //working
    console.log(timestamp); //working
    console.log(smallImage); // This cuts out and breaks my insert. 
    var db = window.openDatabase("MobilePhotos", "1.0", "MobilePhotosData", 1000000);   
    db.transaction(function (tx) {tx.executeSql('INSERT INTO Photos(PhotoID, PictureFile, Longitude, Latitude, Timestamp ) VALUES( '+ timestamp+LID+' ,' + smallImage + '", ' +  longitude +', '+ Latitude +', "'+ timestamp +'")')})
}


function take_pic(LocationID) {
    cameraLocationID=LocationID;
    navigator.camera.getPicture(onPhotoDataSuccess, function(ex) {alert("Camera Error!");}, { quality : 50, destinationType: Camera.DestinationType.DATA_URL });
}

Here is the error I get when I try to input the BLOB:

02-05 16:10:19.702: W/System.err(12028): 
android.database.sqlite.SQLiteException: 
no such column: undefined (code 1): , while compiling:
INSERT INTO OrderPhotos(PhotoID, PictureFile, Longitude, Latitude, Timestamp ) 
VALUES( 1904010821 , "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDABsS..(3825 characters) 

But I don't see the rest of the fields. Could something be breaking the double quotes? Or is something else happening here?

If I put a "1" in for smallImage my output works fine I get:

INSERT INTO OrderPhotos(PhotoID, PictureFile, Longitude, Latitude, Timestamp ) 
VALUES( 1904010821 , "1", 39.10, -84.50, 190401082)

Upvotes: 1

Views: 5001

Answers (1)

ManuParra
ManuParra

Reputation: 1531

did you try use operator ?:

db.transaction(function(tx) {
  tx.executeSql("INSERT INTO OrderPhotos(PhotoID, PictureFile, Longitude, 
                  Latitude, Timestamp ) VALUES
                 ( 1904010821 , ? , 39.10, -84.50, 190401082)", 
   [PictureFile],
   function(tx, res) {
      ....
  });
 });

Operator ? , is better than use strings concatenation.

With your example:

    db.transaction(function (tx) 
{tx.executeSql('INSERT INTO Photos(PhotoID, PictureFile, Longitude, Latitude, Timestamp ) 
VALUES( ?,?,?,?,?)')},[photoid, small photo, longitud, latitude, timestamp])}

Each ? Correspond with each var in the same order.

Upvotes: 3

Related Questions