Not Understanding ? Why do we need BLOB in "Android Sqlite" - android

I am not understanding why do we need BLOB in SQlite. I have seen this explanation in some of the documents
BLOB. The value is a blob of data, stored exactly as it was input.
But I did not get what it is ? and why is it necessary ? Please provide me some suggestions.
Regards
Anand

BLOB is an acronym that means
B inary L arge OB ject
So, it's a container for stuff that you want stored "as such" without the need to serialize them to eg a clean string, and that are typically larger than the usual types that are stored in a database. Examples are serialized objects (a serialized object is represented by a stream of bytes), bitmaps, sound clips, etc.

when need store ordinary object in database like ImageView at that time
blob type use for that

Related

What is the best way to save a string-array in SQLite on Android?

One string size is about 200 bytes,
and it stores 10~20 size in a daily array.
(Store 10~20 strings of 200bytes, as array type)
I have found a way to convert an array to a string
and store it in SQLite.
However, I do not know it's a good idea
because the size of the string is large.
1.
If large arrays of strings,
is it a good idea to store arrays as a string?
2.
or is there a better way?
I would like advice. Thank you.
You're actually placing your concern onto the wrong part of your database design.
For SQLite, the maximum length of a String is 1 billion bytes, so your worries about your 10-20 strings of 200 bytes each actually isn't considered that large.
There's really no harm in storing your array as a single long String in your database. Especially when it's nowhere close to the maximum limit of a String.
Your database query time won't become longer due to your String being long. The real concern here is the processing you'll be doing on that String to turn it back into an Array. Typically, if the String is extremely long, the real performance hit is when you're flattening the array into a String and when you're transforming that String back into an Array.
However, typically, this is something you'll show a loading indicator for to your users.
For storing an Array into a database, there's really only two ways to do so:
Flatten array into a single String and store the String as TEXT
Create a table meant to store the individual elements of the string, and include a column for a Foreign Key that allows you to associate those rows with the same array. Then you'll store each element of your String arrays as a row in this table.
Depending on what you need, one design is better than the other.
For example, you would normally prefer the second implementation if your app requires you to constantly edit individual elements of an array.
For such an example, it wouldn't make much sense to use the first solution, because this means every time you want to edit the contents of an array, you'll be fetching back the complete array in it's entirety. This is impractical when you only want to fetch or edit a particular portion of that String.
Therefore, in such an example, it is much more practical to store the individual elements of the arrays into individual rows of a Table meant for this type of data. You'll be querying only the row you want and updating only the row you want.
So to answer your questions, there's really only two ways to store your String array as a TEXT type in your SQLite database. Both ways work and the real concern is to consider which solution fits your needs best.
If your app only requires you to store and fetch the array in it's entirety each time, then the single String method might be more preferable.
But if your app requires you to work with individual elements of your array, then using the table method would be more convenient.

Android, Write a char array to sharedpreference without creating a String

I was wondering if there is a way to put data in Sharedpreferences on Android without creating a String. I have a sensitive char array(base64 encoding) that I would like to store in an encrypted Sharepreference but I would like to avoid creating a String(using putString()) in the process given that they are immutable. Is there a way to accomplish this?
Thanks!
Well for this scenario I can suggest you can hide actual content with key-value pattern, just create any significant key for your data at point of entry and set data as value for same and save both in SQL-lite or any supported database, and then retrieve same values using key at your destination.
By this way only key will be shared in internal transaction and data will never reveal on any mid-points.
Hope this will help.

Storing a data - Benefit and cons of each storage types

I have this kind of data. This can be or don't be an array. Just for easy reference.
ArrayName = Array1, Array2, Array3
Array1 = abc, cde, fgh
Array2 = abc, cde
Array3 = abc, cde, fgh, ijk, lmn
So, what are the best method to store this kind of data.
If I want to
Add or delete Array1 and all things inside
Add or delete item in Array2(eg. adding fgh or remove cde)
Methods I discovered:
SharedPreference Android Shared preferences example
Arrays
SQLite Android SQLite Example
Text file
Please share the pro and cons of why you choose the method.
Please also share if there are better ways to store this kind of data.
Kindly edit this if you found a better link or sample for other to reference.
Here are pros and cons for each solutions:
1) SharedPreference
You save simple key-value pairs here. So it is very hard to save array and complex structures in SharedPreference. So the solution will not work with arrays and arrays of arrays. It will be extremely(but not impossible) difficult to achieve what you want.
2) Arrays
Absolutely not! It is memory storage, so when you close app, or on process death, you will lose all data
3) SqlLite
I would add to this other Databases for android, like Realm.
Good solution. It is structured storage for collection of data. It will be very easy for storing/retrieving data when it is structured as rows. Furthermore you can delete rows easily. You don't have to read whole structure (other arrays) when you need particular row, or particular array (table in this case)
4) TextFile
I don't recommend to store in a text file, but it is possible to do so, you can serialize those arrays to text file, and deserialize. But every time you have to do this, and to read whole structure and parse it even if you want only e.g. Array2. It can be slow when your data becomes bigger.
It's incredibly hard to give advice with such vague requirements, you apparently have data structured as an array of arrays of strings, and you want to store it persistently on Android - and that's basically all we know.
In addition to the solutions mentioned, I would consider using GSON to store this as JSON to disk. While read/write may not have optimal performance, it's very easy to model documents with things like arrays of arrays, and we have no way of knowing your performance requirements vs ease of use.
class MyData {
public List<List<String>> data;
}
If you then have a MyData object, you could simply serialize it to a string, which could be written to a file on disk:
String json = new Gson().toJson(myData);
This would produce something like
{
"data": [
["abc", "cde", "fgh"],
["abc", "cde"],
["abc", "cde", "fgh", "ijk", "lmn"]
]
}
which could easily be written to disk using e.g. standard File and BufferedWriter. You can then read it back and deserialize using:
MyData myData = new Gson().fromJson(json, MyData.class);

Android store last 10 search values

I have an app that searches in a database. I want to store last 10 search values (and I want them to remain even if the user closes the app) to use them as an adapter in AutoCompleteTextView.
SharedPreferences doesn't seem to support arrays or arraylists. What's the best approach here?
You can use a table in a SQLiteDatabase to store the search history, and use the standard SQL API to access it.
Or you can use a file in XML, JSON, YAML, CSV, plain text, or whatever you like to persist the history. The advantage is simplicity and (maybe) performance. The disadvantage is that you'll have to serialize and deserialize yourself (a possible variation is to serialized a Java object directly)
SharedPreferences support boolean int, float, long and String.
BUT ArrayList are Serializable, so if you also declare you object Serializable, you can encode them into a ByteBuffer with an ObjectOutputStream, then convert this byteBuffer into a String, and finally store it into a SharedPreferences, or better to a binary file (as bytebuffer).
In your case, where you just have to save String, it is easer run through every element of the array and save them as "arrayName"+index, and finally save the size of the array.

Array list of double in SharedPreference

I want to know whether it is possible to save as an array list of double in SharedPreference. In my application, I want the 'size' to be saved whenever there is new 'size' written from the user. It must be uploaded to an array list of double, without erasing the previous one.
first of all - the answer to your question is simple:
it's not possible.
but the good news are that there are lots of ways saving array of doubles (as you've been suggested in the comments):
save it to a binary file / serialized file in the internal/external storage, to SQLite database, make it the responsibility of the server side (if there is one..)
you can save the size of the array in the shared preferences if it helps you read the data from the file containing the serialized array, but I guess you already know that..
anyway - good luck

Categories

Resources