Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 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 /
  • Help Room /
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

avatar image
0
Question by Giusort · Mar 19, 2017 at 01:41 PM · spritedatabasesqlitejpgblob

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
Add comment · Show 2
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 RaviRK · Mar 13, 2018 at 06:22 AM 0
Share

please share the code about how to store the images in sqlite.

avatar image Giusort RaviRK · Mar 14, 2018 at 02:31 PM 0
Share

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!

0 Replies

  • Sort: 

Follow this Question

Answers Answers and Comments

119 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Size of PNG JPG Images is x10 original size. 0 Answers

Problem with sqlite android [Table not found] 1 Answer

How To Read data in SQLite4Unity3D using IDataReade? 0 Answers

Error Inserting Record - SQLite Database 0 Answers

Sqlite On Android Does not work with unity 0 Answers


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