I have a question about SQLite. I'm trying to save a custum objects (let's say ObjectA) on my phone's db. ObjectA contains an ArrayList.
I'll probably have to save the images as BLOBs but how should I design my database. Should I create a new table with all my pictures (BLOB byte[], INTEGER objectA_row_id) ? or is there another way to make this association?
May be create table with path to the pictures.
You shouldn't store file path directly in database, because if you delete the file then you will not get image, so you have 2 options:
1) Store image to your cache/internal and then save its file path.
2) Use database table containing BLOB type.
Related
Can I add books to my SQLite database in android??
If yes, how?
I want to put the books on database because I've too many books to add to the android app and I don't want to make the size of the Application too much.
There's a limit of 1MB on internal assets due to dynamic decompression; the 1MB >limit also seems to apply to Cursor blobs but this doesn't seem to be >documented anywhere.
Generally you should avoid blobs in SQLite as they perform poorly; instead, >save the blob data as a file and store the location of the file in your DB
I won't recommend storing pdf flies in SQLite, you can just provide a simple link to download it to the user. but if you want to store it here you can do something like this.
First, you need to convert the pdf file into a byte array
import java.nio.file.*;
Path pdfFilePath = Paths.get("/file/path/your_file.pdf");
// Read file to byte array
byte[] pdfByteArray = Files.readAllBytes(pdfFilePath );
// Write byte array to file
Files.write(pdfFilePath , pdfByteArray);
You're looking for BLOB to store the data in android SQLite.
BLOB - The value is a blob of data, stored exactly as it was input.
Here's an example of making a table with two columns - an id and some data, which is a BLOB:
just for an example, you can create a table like this
CREATE TABLE t1 (id INTEGER PRIMARY KEY, data BLOB);
You can't do it. you cant store files in SQLite database. You will store your files in asserts folder.
I want to save image using my developed app into mobile what is the best way to it.
I was trying to save via sqlite but is there any other options?
The recommended way is to store the image as a file not in the database and then store the path or enough of the path to uniquely identify the image in the database.
To store an image you have to store it as a BLOB based upon a byte[] (byte array);
However, using the Android SDK, there is a limitation that only images less than 2MB can be retrieved even though they can be saved.
Technically you could store the image as a hex, binary, octal string but that would be more complex, less efficient and even more restrictive.. So really you have to store it as a BLOB is not completely factual.
It's also actually pretty useless storing images (or any large files) with SQLite or most structured databases as there is little that you can do with the data query wise.
You actually save a BLOB via SQL using something like :-
INSERT INTO your_table VALUES(x'00FF01EF');
i.e. 00 (0) is the first byte, FF (255) the 2nd, 01 (01) the 3rd .........
note the above would only work for a table with a single column
However, the SQLiteDatabase insert convenience method does the conversion from the byte[] to the correct SQL (hex string) on your behalf.
So you'd typically use :-
ContentValues cv = new ContentValues();
cv.put("your_column",your_image_as_a_byte_array);
your_sqlitedatabase_object.insert("your_table",null,cv);
You retrieve the byte array from a Cursor (result of a query) using something along the lines of
your_retrieved_image_byte_array = your_cursor.getBlob(your_column_offset_as_an_int); //<<<<<<<<< CursorWindow full error if the row cannot fit into the CursorWindow which has 2MB limit
You may wish to have a look at How can I insert image in a sqlite database, as this goes into more detail and has code that stores both images and paths (either one is stored based upon the image size, less then 100k, then the image is stored, else the path is stored) and additionally retrieves the images into a ListView. Additionally, although not recommended this is a way of getting around the 2Mb limit How to use images in Android SQLite that are larger than the limitations of a CursorWindow?
I have 600+ text files. How do I put them in a database, with mySQL lite.
I want to have 3 columns: index(1 to 600), title and content.
How do I do that? Thanks.
http://www.sqlite.org/datatype3.html
When you create the database (the single table in it), the index should be auto-generated when the entry is created in the table (if you properly created the table with auto indexing)
I would use TEXT (varchar) for the title & blob for the actual text file (the data in it).
The previous answer seems to put you on the right track, though I'm not sure I quite understand how you intend to actually populate the table with the data.
Use content provider for android below is nice link.
http://www.vogella.com/tutorials/AndroidSQLite/article.html.
For datatype to use integer,text,text as columns, below is
datatype link of sqlite.
http://www.sqlite.org/datatype3.html
But Instead of storing the file data in sqlite, I would suggest you to store sdcard path of files in sqlite table and retreive from sdcard when required.
In above case columns will be index,title,filepath.
I am new to android so have very less idea about working of images in sqlite db. I am creating an app in which i will be creating a db with pre-filled data that means i would be putting that data-filled db in assets folder and user will start using it directly once the app is installed.
So i have around 186 images of flags from different countries, which i need to show in a custom listview. Can you tell me how to store all images in sqlite db and get it back to show on listview?
I tried using some IDE for inserting images in db but their decoding doesn't work.
Please help!!!
try this First of all you have to store all images in blob datatype into sqlite database then for retrieving from database user following code
query_compnay_id="Select * From "+mStaticValues.company_master;
mCursor_company_detail=mDatabaseConnectionAPI.ExecuteQueryGetCursor((query_compnay_id));
mCursor_company_detail.moveToFirst();
byte[] by =mCursor_company_detail.getBlob(mCursor_company_detail.getColumnIndex(mStaticValues.image_data));
mBitmap=BitmapFactory.decodeByteArray(by, 0, by.length, null);
I am dealing with ECG signal processing using Android phone. I am getting the filtered ECG signal after the series of operations as my final output, and it is saved in text file in particular location. The text file contains the values of voltages in a single column, containing 30000 samples. This is done for number of patients.
Now I have to use these text files in my database with particular patients, as a entry in their row.
I am using SQLite database.
How can it be done?
please help me with this.
Read the file and save it as blob in sqlite, but if data is too large, you will have problems with size. Another way would b to keep the files in sdcard with some id as filename, you could save these ids in db which you can refer.
OK basically what you need to do is:
bulkinsert your data.
here is the link on how to do it:
http://notes.theorbis.net/2010/02/batch-insert-to-sqlite-on-android.html
if you need to populate your second table with this data then
update yourtable set blah=(select field from yourfreshlypopulatedtable)