简体   繁体   English

使用HTML5和JavaScript在SQLite中插入和检索BLOB图像

[英]Insert and retrieve BLOB image in SQLite with HTML5 and JavaScript

i have this short javascript code where i can input record to sqlite database: 我有这段简短的javascript代码,可以在其中将记录输入到sqlite数据库:

  function insertRecord() // Get value from Input and insert record . Function Call when Save/Submit Button Click..

  {

  var namamenutemp = $('input:text[id=namamenu]').val();

  var satuantemp = $('input:text[id=satuan]').val();

  var hargatemp = $('input:text[id=harga]').val();

  var stoktemp = '0';
  db.transaction(function (tx) { tx.executeSql(insertKasir, [namamenutemp, satuantemp, hargatemp], loadAndReset, onError); });  
  }
$("#submitButton").click(insertRecord);

my html to input to sqlite: 我的HTML输入到sqlite:

<input type="hidden" id="id">
<input id="namamenu" type="text" placeholder="Nama menu" required="" autofocus="" pattern=" ">
<input id="satuan" type="text" placeholder="Contoh: PCS" required="" pattern=" ">
<input id="harga" type="text" placeholder="Rp." required="" pattern=" ">
<button id="submitButton" type="submit" >Save</button>

and this is my table structure : 这是我的表结构:

"CREATE TABLE IF NOT EXISTS Kasir (id INTEGER PRIMARY KEY AUTOINCREMENT, namamenu TEXT, satuan TEXT, harga TEXT)"

and now i want to insert some image to my table, and i've found it will using BLOB, but i don't know how to use it on my HTML5-Javascript. 现在我想在表中插入一些图像,并且我发现它将使用BLOB,但是我不知道如何在HTML5-Javascript上使用它。

do i have to change my table structure too? 我也必须更改表结构吗?

The is a similar question: how to get the contents of an image as base64. 这是一个类似的问题:如何将图像的内容获取为base64。 Looks like the method is specific to firefox, but could also work in other browsers: Get image data in JavaScript? 看起来该方法特定于firefox,但也可以在其他浏览器中使用: 用JavaScript获取图像数据?

If the image is on the same domain, you could get its content with ajax. 如果图像在同一域中,则可以使用ajax获取其内容。 Since you're already using jQuery it would look like this: 由于您已经在使用jQuery,因此它将如下所示:

$.get(imageUrl, function(result) {
  console.log('binary image content', result);
});

If you want to show the image, you need to turn its content to base64, like described here: Embedding Base64 Images 如果要显示图像,则需要将其内容转换为base64,如此处所述: 嵌入Base64图像

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

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