This question was
closed Mar 19, 2017 at 01:59 PM by
Giusort for the following reason:
The question is answered, right answer was accepted
SQLite with JPG blob
Hi!
I've a SQLite DB with a blob field in which I loaded JPG images. I use this code to retrieve data and use as a sprite:
GetImageTexture("SELECT blob FROM 'images' WHERE id='" + id +"' AND nomefile='" + ImgFileName +"'", out BlobTexture, out BlobWidth, out BlobHeight);
GameObject.Find ("ContentImage").GetComponent<UnityEngine.UI.Image> ().sprite = Sprite.Create(BlobTexture, new Rect(0,0,BlobWidth,BlobHeight), new Vector2 (0.5f,0.5f));
Where GetImageTexture and GetBlobData are:
void GetImageTexture(string Query, out Texture2D tex, out int width, out int height) {
tex = new Texture2D (1024, 1024);
tex.LoadImage (GetBlobData (Query));
width = tex.width;
height = tex.height;
}
public static byte[] GetBlobData(string Query){
byte[] BlobContent = new byte[0];
dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = Query;
reader = dbcmd.ExecuteReader();
while (reader.Read()) {
BlobContent = (byte[])reader["blob"];
}
return BlobContent;
}
It works fine if I use PNG format for images but not for JPG images in SQLite DB. Where I'm wrong?
Thank you!
[EDIT] IT WORKS! It was only a wrong query with no result! I leave the question&answer for community, it can be useful.
Comment
please share the code about how to store the images in sqlite.
So, to store images you have to use:
public static bool SaveBlobData(String tableName, byte[] BlobContent) {
string query;
query = "INSERT INTO " + tableName + "(blob) VALUES (@BlobContent)";
try {
dbcmd = dbcon.CreateCommand();
dbcmd.CommandText = query;
SqliteParameter setParam = new SqliteParameter("@BlobContent", BlobContent);
dbcmd.Parameters.Add(setParam);
dbcmd.ExecuteNonQuery ();
} catch(Exception exception) {
Debug.LogError(exception.$$anonymous$$essage);
return false;
}
return true;
}
Don't you forget to open the DB before saving data in. Hope this help!