SQLite, BLOB and UI Image

Hi people!

It’s possible to retrieve UI Image data from the BLOB field of a SQLite DB?

Thank you!


A BLOB field in sql contains the binary data of an image.
This binary data can be converted to a Texture2D in Unity and the conversion code is already inside the Unity API!

Take a look at this page for more guidelines: http://docs.unity3d.com/ScriptReference/Texture2D.LoadImage.html

Or this page to load raw texture data:

Just make sure you retreive the BLOB data from your database as a byte array!


I solved this way, assuming the query returns just one image:

void GetImage() {
  string connectionString = "URI=file:" + string.Format(@"Assets/StreamingAssets/{0}", "SQLiteDB.db");
  IDbConnection dbcon = new SqliteConnection(connectionString);
  IDbCommand dbcmd;
  IDataReader reader;
  var tex = new Texture2D(64,64);

    string sql = "SELECT image FROM dabname where ID='1'";

    dbcmd = dbcon.CreateCommand();
    dbcmd.CommandText = sql;
    reader = dbcmd.ExecuteReader();

    while (reader.Read()) {
      byte[] img = (byte[])reader["image"];
      // image 266x199
      GetComponent<Image>().sprite = Sprite.Create(tex, new Rect(0, 0, 266, 199), new Vector2(0.5f, 0.5f));

dbcon.Close ();
reader.Dispose ();