如何在sqlite中插入图像

How to insert image in sqlite?

本文关键字:插入 图像 sqlite      更新时间:2023-09-26

我正在使用cordova开发一个跨平台应用程序
我需要在sqlite中插入图像。我得到了很多android的代码,但我发现很难使用javascript。当我在iPhone中运行以下代码时,我得到了err.code:5

document.addEventListener("deviceready", onDeviceReady, false);
var img;
var currentRow;
var b = new Blob();
function previewFile() {
    // var preview = document.querySelector('img');
    var file    = document.querySelector('input[type=file]').files[0];
    var reader  = new FileReader();
    // var package_name = document.getElementById("pr").value;
    reader.onloadend = function () {
        // img = reader.result;
        if(file.type.match('image.*'))
        {
            img = reader.result;
            // ref.push({"image":image,"service":arr,"package_name":package_name});
        }
        else
        {
            alert("select an image file");
        }
    }
    if (file) {
        reader.readAsDataURL(file);
    } else {
        preview.src = "";
    }
    var image1 = encodeURI(img);
    // var b = new Blob();
    b = image1;
    console.log(b);
    console.log(image1);
    //document.write('<img src="'+image+'"/>');
}
function onDeviceReady() {
    var db = window.sqlitePlugin.openDatabase({name:"sqlite"});
    db.transaction(populateDB, errorCB, successCB);
}
function populateDB(tx) {
    tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id INTEGER PRIMARY KEY AUTOINCREMENT, name,number,image BLOB)');
}
function insertDB(tx) {
    tx.executeSql('INSERT INTO DEMO (name,number,image) VALUES ("' +document.getElementById("txtName").value
                    +'","'+document.getElementById("txtNumber").value+'","' +b+ '")');
}
function goInsert() {
    var db = window.sqlitePlugin.openDatabase({name:"sqlite"});
    db.transaction(insertDB, errorCB, successCB);
}

我的html代码:

<input type="file" onchange="previewFile()">    
<button onclick="goInsert()">Insert</button>

如何做到这一点。有人能帮我吗?提前感谢。。。

将图像(在内存中)转换为byte[],然后将其保存为varbinary(max)。

将blob插入数据库会使您的应用程序既慢又滞后,而是将所选图片的路径保存到数据库中。

当您想将图像上传到服务器时,请使用cordova的文件上传功能。

如果您从服务器获取图像,请在本地下载该图像并将该路径保存到数据库中

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
    function onFileSystemSuccess(fileSystem)
    {
        fileSystem.root.getFile(
            "dummy.html", {create : true, exclusive : false},
            function gotFileEntry(fileEntry)
            {
                var sPath = fileEntry.fullPath.replace("dummy.html", "");
                var fileTransfer = new FileTransfer();
                fileEntry.remove();
                fileTransfer.download(
                    "http://www.w3.org/2011/web-apps-ws/papers/Nitobi.pdf",
                    sPath + "theFile.pdf",
                    function(theFile)
                    {
                        console.log("download complete: " + theFile.toURI());
                        showLink(theFile.toURI());
                    },
                    function(error)
                    {
                        console.log("download error source " + error.source);
                        console.log("download error target " + error.target);
                        console.log("upload error code: " + error.code);
                    }
                );
            }, fail);
    }, fail);