简体   繁体   English

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

[英]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. 嘿大家一直在研究一个项目来拍照并使用phonegap / jQuery / html5将它们存储在数据库中。 The database entry needs to store some geolocation information and the BLOB of the image that is taken by the camera. 数据库条目需要存储一些地理位置信息和摄像机拍摄的图像的BLOB。 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. BLOB总是打破我的插入语句。 If I set the smallImage to "1" instead of the image data it works fine. 如果我将smallImage设置为“1”而不是图像数据,它可以正常工作。 Is there a better way to insert this blob? 有没有更好的方法来插入这个blob? When I look at the log it looks like the smallImage is cut off. 当我查看日志时,看起来像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 });
}

Here is the error I get when I try to input the BLOB: 这是我尝试输入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: 如果我为smallImage输入一个“1”我的输出工作正常我得到:

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

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. 与每个var对应的顺序相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM