简体   繁体   English

Flex本地SQLite BLOB显示

[英]Flex local SQLite BLOB display

I'm building an application from Flash Builder 4.5. 我正在从Flash Builder 4.5构建应用程序。 I'm a newb to SQLite and here's where my questions come in! 我是SQLite的新手,这就是我的问题所在!

I've got 3 columns in an SQLite db I built with Firefox's handy SQLite Manager. 我使用Firefox的方便SQLite Manager构建的SQLite数据库中有3列。 So, I created 3 columns, an index/reference that's just an incrementing INTEGER for easy reference, a descriptive title of the image (so it can be searchable later) in a TEXT, and a BLOB type that holds the pngs. 因此,我创建了3列,一个索引/引用只是一个递增的INTEGER以便于参考,一个描述性的图像标题(因此可以在以后搜索)在TEXT中,以及一个保存png的BLOB类型。

I'm trying to add this in an assets folder into my project (as a pre-populated database) and display the blob images, one at a time, in a window one after the other, scrollable by user. 我正在尝试将这个资源文件夹中的这个添加到我的项目中(作为预先填充的数据库)并一个接一个地在一个窗口中一个一个地显示blob图像,用户可以滚动。 This is where I'm running into problems. 这是我遇到问题的地方。 Eventually I want to be able to encrypt it and add a search tool, but first things first, and that's displaying an image. 最终我希望能够加密它并添加一个搜索工具,但首先要做的就是显示图像。

I have spent several hours scouring the web about this topic, but there isn't much help out there: Adobe has tips about using SQLite with AS3 (their reference documents and the support section of their website have some overview articles) but nothing refers to reading blob data in depth. 我花了几个小时在网上搜索这个主题,但没有太多帮助:Adobe有关于将SQLite与AS3一起使用的提示(他们的参考文档和他们网站的支持部分有一些概述文章)但没有提到深入阅读blob数据。

On this site, I've found some references to Adobe Air looking for extra byte information when reading blobs since they have a different syntax to write to the databases created within the program. 在这个网站上,我发现一些参考Adobe Air在读取blob时寻找额外的字节信息,因为它们有不同的语法写入程序中创建的数据库。 See: What is the first bytes in a Blob column SQlite Adobe AIR? 请参阅: Blob列SQlite Adob​​e AIR中的第一个字节是什么? Blob Sizeinfo? Blob Sizeinfo?

Someone referenced the need to use CAST in the SELECT statement to use dbs not created by AIR, but adobe's website doesn't have any information about this in their documentation. 有人引用了在SELECT语句中使用CAST来使用不是由AIR创建的dbs的需要,但是adobe的网站在他们的文档中没有关于此的任何信息。

Anyway, I've rambled a bit, but I'm a bit overwhelmed - Any help/insight is greatly appreciated. 无论如何,我有点絮絮叨叨,但我有点不知所措 - 任何帮助/洞察力都非常感激。

Insert Code: 插入代码:

insertStatement = new SQLStatement();
insertStatement.sqlConnection = connection;
insertStatement.addEventListener(SQLEvent.RESULT, onInsertResult);
insertStatement.addEventListener(SQLErrorEvent.ERROR, onInsertError);
insertStatement.text = "INSERT INTO my_table (title, imagedata) VALUES (@titleString, @imageByteArray)";
insertStatement.parameters["@titleString"] = pngTitle; // String containing title
insertStatement.parameters["@imageByteArray"] = pngByteArray; // ByteArray containing image
insertStatement.execute();

Retrieval Code: 检索代码:

selectStatement = new SQLStatement();
selectStatement.sqlConnection = connection;
selectStatement.addEventListener(SQLEvent.RESULT, onSelectResult);
selectStatement.addEventListener(SQLErrorEvent.ERROR, onSelectError);
selectStatement.text = "SELECT title, CAST(imagedata AS ByteArray) AS imagedata FROM my_table WHERE id = @recordId;"; 
selectStatement.parameters["@recordId"] = targetRecordId; // Id of target record
selectStatement.execute(); 

...

function onSelectResult(event:SQLEvent):void {
    selectStatement.removeEventListener(SQLEvent.RESULT, onSelectResult);
    var result:SQLResult = selectStatement.getResult();
    if (result.data != null) {
        var row:Object = result.data[0];
        var pngByteArray:ByteArray = result.data[0].imagedata;
        var pngTitle:String = result.data[0].title;
    }
}

If you're still having issues should also try encoding the data into base64 prior to insertion and likewise decoding from base64 after retrieval. 如果您仍然遇到问题,还应尝试在插入之前将数据编码到base64,并在检索后同样从base64解码。 This will add an additional ~40% to the size of data insert to the database but it can help eliminate issues when using binary data with SQLite. 这将为数据库插入到数据库的大小增加约40%,但它可以帮助消除在使用SQLite的二进制数据时出现的问题。

Follow Up Questions: 跟进问题:

I didn't see a reference to my database in an assets/resource folder from within the application. 我没有在应用程序内的资源/资源文件夹中看到对我的数据库的引用。

If you had an folder called assets in your default package directory and it contained your prefilled database file mydb.sqlite (for example), you could open the database as follows: 如果在默认包目录中有一个名为assets的文件夹,并且它包含预mydb.sqlite数据库文件mydb.sqlite (例如),则可以按如下方式打开数据库:

var dbf:File = File.applicationDirectory.resolvePath("assets/mydb.sqlite");
var connection:SQLConnection = new SQLConnection();
connection.open(dbf);

Do I have to build it from the code as such? 我是否必须从代码中构建它?

You don't have to create the database from code - in fact I generally use SQLite Manager to create SQLite databases. 您不必从代码创建数据库 - 实际上我通常使用SQLite Manager来创建SQLite数据库。 However, since you're using BLOB data it would be best practice to populate the database in code. 但是,由于您使用的是BLOB数据,因此最好在代码中填充数据库。


Does that mean I need to have my images in an assets folder (or on a remote server, etc) just to populate the database? 这是否意味着我需要将我的图像放在资产文件夹(或远程服务器等)上才能填充数据库?

The images can be local or remote. 图像可以是本地的或远程的。 You just need to be able to load them into a byte array for storage. 您只需将它们加载到字节数组中即可存储。 But if you have the option of a separate image repository it would be better to store links/paths to the images instead of storing the binary data within the database. 但是,如果您可以选择单独的图像存储库,那么最好将链接/路径存储到图像中,而不是将二进制数据存储在数据库中。

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

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