Phonegap拍照并将图像(BLOB)插入数据库SQLite

时间:2013-02-05 21:33:22

标签: javascript jquery sqlite cordova

嘿大家我一直致力于一个项目,使用phonegap / jQuery / html5拍照并将它们存储在数据库中。数据库条目需要存储一些地理位置信息和摄像机拍摄的图像的BLOB。这是我用来尝试这样做的当前方法,它不适合我。 BLOB总是打破我的插入语句。如果我将smallImage设置为“1”而不是图像数据,它可以正常工作。有没有更好的方法来插入这个blob?当我查看日志时,看起来像smallImage被切断了。

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

以下是我尝试输入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) 

但是我没有看到其余的领域。有什么东西可以打破双引号吗?或者是其他事情发生在这里?

如果我在smallImage中输入“1”,我的输出就可以了:

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

1 个答案:

答案 0 :(得分:3)

您尝试使用运算符?

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) {
      ....
  });
 });

运算符?,优于使用字符串连接。

用你的例子:

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

每个?对应于相同顺序的每个var。