PDF files on SQLite database - android

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.

Related

Saving png image in database through android app

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 would like to create a database containing 600 text files for an android Application

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.

SQLite ArrayList<Bitmap> Android

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.

Retrieve large blob from Android sqlite database

I stored chunks of binary data (protobufs) in the sqlite database of an Android app without realizing that Android's Cursor can only hold a maximum of 1MB of data. I now know that I should have stored these binary blobs in files and only referenced the files in the sqlite database entries.
I need to upgrade the database (the app has been in use for a while) in order to move these binary chunks to files. The problem is that some user's data may have already exceeded the 1MB limit and I'm not able to retrieve it from the database (accessing the resulting Cursor for a single row that contains a large blob throws an IllegalStateException: Couldn't read row 0, col 0 from CursorWindow. Make sure the Cursor is initialize before accessing data from it).
How do I retrieve the binary blobs that are greater than the 1MB Cursor limit that have been stored in the sqlite database?
You can read large blobs in pieces. First find out which ones need this treatment:
SELECT id, length(blobcolumn) FROM mytable WHERE length(blobcolumn) > 1000000
and then read chunks with substr:
SELECT substr(blobcolumn, 1, 1000000) FROM mytable WHERE id = 123
SELECT substr(blobcolumn, 1000001, 1000000) FROM mytable WHERE id = 123
...
You could also compile your own copy of SQLite and access either the BLOB stream I/O functions or the normal query functions of the C API with the NDK, but that would be too complex in this case.
CL. answer will only with blobs<5MB. If you tried to use it with blobs bigger than 5 megabytes you will still get the exception. To fetch large blobs you need to use a library called sqlite4java that uses native calls to the database without using cursors. Here is an example of using this library to fetch a large blob:
SQLiteConnection sqLiteConnection=null;
SQLiteStatement sqLiteStatement=null;
try
{
File databaseFile = context.getDatabasePath("database.db");
sqLiteConnection=new SQLiteConnection(databaseFile);
sqLiteConnection.open();
sqLiteStatement=sqLiteConnection.prepare("SELECT blob FROM table WHERE id=?");
sqLiteStatement.bind(1, id);
sqLiteStatement.step();
byte[] blob=sqLiteStatement.columnBlob(0);
}
finally
{
if(sqLiteStatement!=null)
sqLiteStatement.dispose();
if(sqLiteConnection!=null)
sqLiteConnection.dispose();
}

Using text file in SQLite

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)

Categories

Resources