Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Giusort · Apr 12, 2016 at 04:41 PM · c#imagedatabasesqliteblob

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!

Gius

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by M-Hanssen · Apr 12, 2016 at 07:25 PM

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: http://docs.unity3d.com/ScriptReference/Texture2D.LoadRawTextureData.html

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Apr 13, 2016 at 12:10 AM 0
Share

Also make sure the image is in a supported format. Unity can only load .jpg and .png at runtime.

avatar image Giusort · Apr 14, 2016 at 02:19 PM 0
Share

Thank you Hanssen!

I tried ExampleScript you suggested and it works on a plane but not on a UI Image (with $$anonymous$$esh Renderer on it), as you can see on screenshot.

Where I'm wrong?

Thank you

$$anonymous$$

alt text

screenshot.png (339.7 kB)
avatar image Giusort · Apr 19, 2016 at 03:44 PM 0
Share

Solved this step simply changing the sprite:

GetComponent().sprite = Sprite.Create(tex, new Rect(0, 0, 64, 64), new Vector2(0.5f, 0.5f));

avatar image ramin1000 · Apr 11, 2017 at 02:31 PM 0
Share

What components must add to game object to above? above pic not clean. Thank you

avatar image Giusort ramin1000 · Apr 14, 2017 at 07:47 AM 0
Share

Hi Ra$$anonymous$$,

the important thing are mesh renderer and TestImg.cs, containing the function GetImage() below.

avatar image
0

Answer by Giusort · May 04, 2016 at 12:29 PM

EDIT

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);
 
   dbcon.Open();
     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"];
       tex.LoadImage(img);
       // image 266x199
       GetComponent<Image>().sprite = Sprite.Create(tex, new Rect(0, 0, 266, 199), new Vector2(0.5f, 0.5f));
     }
 
 dbcon.Close ();
 reader.Dispose ();
 }
 

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image charlesjunior · Mar 01, 2017 at 05:27 PM 0
Share

Thank you, it worked here. Helped me a lot.

avatar image solloranoharold123 · Mar 03, 2019 at 04:22 AM 0
Share

Could you help me. Step by Step procedure . I 'm newbie in using unity

avatar image solloranoharold123 · Mar 03, 2019 at 04:23 AM 0
Share

How do I display the image using Sprite.Create

avatar image solloranoharold123 · Mar 03, 2019 at 04:25 AM 0
Share

Its a Null after code it

avatar image Giusort solloranoharold123 · Mar 04, 2019 at 11:40 AM 0
Share

Probably because the tex var is null or empty. Start checking the SQLite DB and the blob field, then the DB connection.

Do you have some some error on dbcon.Open() ?

avatar image solloranoharold123 Giusort · Mar 07, 2019 at 02:53 PM 0
Share

There's no error on it. However the var tex is always null after I fetch an Image in SQLite.

Can you give me some examples oh how to load and save blob files in SQLIte.

avatar image unity_fiS-X9HAH5Am3A · Jan 29, 2020 at 07:00 PM 0
Share

the image shows a red question mark, even tho the byte array is not empry and i checked the boolean returned from tex.loadImage() returns true.

here's my code :

 private void readQuestionsFromDB(){
  
      string conn = "URI=file:" + Application.dataPath + "/quizdb.s3db"; //Path to database.
   IDbConnection dbconn;
   dbconn = (IDbConnection) new SqliteConnection(conn);
   dbconn.Open(); //Open connection to the database.
   IDbCommand dbcmd = dbconn.CreateCommand();
   string sqlQuery = "SELECT id, statement, answer, image " + "FRO$$anonymous$$ questions";
   dbcmd.CommandText = sqlQuery;
   IDataReader reader = dbcmd.ExecuteReader();
  
   while (reader.Read())
   {
      string statement = reader.GetString(1);
  
      answerint = reader.GetInt32(2);
  
      byte[] img = (byte[])reader["image"];
  
      Question q = new Question(statement, answer, img);
      questions.Add(q);
  
   }
   reader.Close();
   reader = null;
   dbcmd.Dispose();
   dbcmd = null;
   dbconn.Close();
   dbconn = null;
  
  }


and then i write this in the start method to display the array as an image :

  readQuestionsFromDB();  
  
     statement.text = questions[0].statement;
  
     Texture2D tex = new Texture2D(800,400); //image is 800/400
      tex.LoadImage(questions[0].image);
      image.GetComponent<Image>().sprite = Sprite.Create(tex, new Rect(0,0,tex.width,tex.height),new Vector2(0.5f, 0.5f));

please any help is appreciatied, thank you!

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to get a database with information in it to be usable in a build 0 Answers

Image showing as question mark after loading from SQLite database 0 Answers

Unity 4 Android Game cannot access SQLite DB (C# Script) 1 Answer

Can't display image from URL using MySQL 000webhost 2 Answers

How to send sqlite database file created by the game to server in unity android? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges