How to: Adding a bookmark feature with SQLite Database - android

I've been thinking of ways to make this work, and I just dont get it.
I have several HTML files that are viewed using webView, I want to create a bookmark button on this WebView page.
If I create a DB with a table and three columns - containing the _id, the Title, and the URL, and populate it with the Titles and the URLs(from assets folder) and give each an IDs. When the "Add to Bookmark" button is clicked in the WebView activity, can it populate a ListView in the Favorites activity, and when the title is clicked, it locates the URL that corresponds in the SQLDB and then open it.
I have googled on SQLDB, but most of the tutorials do not make use of an already stored data in DB.
Thank you.

Alright use this code :
// Saving the bookmark via ContentProvider
final ContentValues bookmarkValues = new ContentValues();
bookmarkValues.put(Browser.BookmarkColumns.TITLE, title);
bookmarkValues.put(Browser.BookmarkColumns.URL, url);
bookmarkValues.put(Browser.BookmarkColumns.BOOKMARK, 1);
final Uri newBookmark = getContentResolver().insert(Browser.BOOKMARKS_URI, bookmarkValues);
// Retrieving
Cursor cursor = getContentResolver().query(Browser.BOOKMARKS_URI,
null, null, null, Browser.BookmarkColumns.CREATED);
cursor.moveToFirst();
final String stitle = cursor.getString(cursor.getColumnIndex("title"));
final String surl = cursor.getString(cursor.getColumnIndex("url"));

Related

Android create second database after first database is created

So I created first database, and now I need to create separate database, zipped it and send it to server.
I'm just wondering if it is doable?
And how can I do it?
Here's the basic logic of what you want to do, it'll have to be supplemented with specific code from here: https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html
and here: https://developer.android.com/reference/android/database/Cursor.html
and here: https://developer.android.com/reference/android/content/ContentValues.html
SQLiteDatabase database1 = SQLiteDatabase.openDatabase(<PATH TO DATABASE1>, null, 0);
SQLiteDatabase database2 = SQLiteDatabase.openOrCreateDatabase(<WHEREVER YOU WANT DATABASE2 TO GO>, null);
Cursor cursor = database1.query(<You'll really just have to fill this in with what you're pulling from database1, it's way too specific>);
ContentValues contentValues;
while (cursor.moveToNext()) {
contentValues = new ContentValues();
contentValues.put(<Column key>, <Column value from cursor>)
// I.E. contentValues.put(ID_KEY, cursor.getString(cursor.getColumnIndex(ID_KEY));
// <INSERT THE REST OF YOUR COLUMNS INTO contentValues>
database2.insert(<TABLE_NAME>, null, contentValues);
}
cursor.close();
database1.close();
database2.close();
To send it, that depends on way too many things for me to address. You have the database file now, send it using whatever protocol you please. If you don't know how to do this, search for "Android send File to Server" (try with an without quotes).

Android and SQLlite: escape quote and get it back

I have to save some data inside of SQLLite. This text data can contain a quote ('). Is there a way to escape this char on insert, and get it back when getting the data from the database?
In particular, the name is a references to a file. So the file can be named like "hel'lo.file". Before escaping it to the database, it should be "hel''lo.file". But when i get it back i need again "hel'lo.file" to be certain that the string inside the db matches the file name.
I'm using a content provider and a SQLiteOpenHelper.
Inside my content provider, for the insert i'm doing this:
_id = db.insert(TextEditorContract.NoteEntry.TABLE_NAME, null, values);
My insert inside my activity:
ContentValues values = new ContentValues();
...
values.put(NoteEntry.COLUMN_TITLE, getFileTitle());
Uri recordUri = contentResolver.insert(NoteEntry.CONTENT_URI, values);
Use SQLiteOpenHelper - then you can use prepared statements. See this question: Android SQLite Example
EDIT
String file = "/storage/emulated/0/Note/hello 'world.txt"
String sql = "SELECT _id FROM Recents WHERE percorso=? ORDER BY _id ASC";
String[] data = {file};
Cursor cursor = database.rawQuery(sql, data);

Unable to show updated values from Cursor when live updating SQLite database

I am fairly new to Android and trying to learn how things work module by module. Here's what I am trying to do:
Show a word with a favorite checkbox (image). If a user taps on it then the database is updated and a column in database table stores its value (1 for checked, 0 for unchecked). I am using a cursor to retrieve values of both the word and favorite checkbox. Tapping on the favorite image correctly updates the database without any problem.
The problem I am facing is:
Unless I exit the application and start it again, the cursor doesn't fetch the recent changes made to the database. To explain it further, when I navigate to the next/previous word (using a button at the bottom of screen) the values retrieved aren't the latest ones i.e it seems like the cursor still has the old database values and not the updated ones.
I did search through Google, StackOverflow to get a concrete solution but it seems like I am not using the right search terms. I know this has something to do with updating cursor and the fact that requery is depreciated but again I have lost direction.
[EDIT] Using the below mentioned method to get Cursor:
public Cursor getWords() {
SQLiteDatabase db = getReadableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String[] sqlSelect = {"_id", "word", "favourite"};
String sqlTables = "word_list";
qb.setTables(sqlTables);
Cursor c = qb.query(db, sqlSelect, null, null,
null, null, null);
c.moveToFirst();
return c;
}
This method is called when user taps on favorite image to update the database:
public void setFavWord(int markFav, int wordPos) {
SQLiteDatabase db = getWritableDatabase();
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
ContentValues values = new ContentValues();
values.put("favourite", markFav);
db.update("word_list", values, "_id = " + wordPos, null);
}
A cursor is not dynamic; it shows a snapshot of the database at the time the query was executed.
When the database changes, you must execute the query again.
As suggested by #Sreekanth in comments section, I am updating the cursor whenever favorite image is tapped. Although it is working just fine but I think it as a workaround rather than a solution. Maybe I am wrong in saying so.

simple code, but cursor is still returning null from content provider

Got a problem here with a simple section of code that uses managedQuery cursors. Two parts, the top half of the code puts a string into the LATITUDE column of the MediaStore database content provider.
The second part of the code below that reads that same string back from the database. This is where it is returning a null result. Either because the string was not correctly read into the database in the first part of the code or there is an error in the second part where it reads it back from the database.
I am using the LATITUDE column of the Media.images content provider to store a string. There is no other unused column that is available so that is why I am using it.
The goal is to put the string path name of the mp3 file into the LATITUDE column of an image and read it back out again later with another query.
I tracked down the problem to the following code. The cursor in the second part of code is returning null. Is there something wrong with my use of cursors, or some error in this that I don't know about?
String displayName; // string pathname of the mp3 file to be put into LATITUDE column
String filename; // the pathname of the image that I want to add the database info to
ContentValues imageValues = new ContentValues();
String selection3 = MediaStore.Images.Media.DATA + "='" + filename +"'";
imageValues.put(MediaStore.Images.Media.LATITUDE, displayName);
getContentResolver().update(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
imageValues, selection3, null);
String[] proj6 = { MediaStore.Images.Media.LATITUDE };
String selection6 = MediaStore.Images.Media.DATA + "='" + filename +"'";
Cursor cursor2 = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
proj6, selection6, null, null);
cursor2.moveToFirst();
String displayer = (String)
cursor2.getString(cursor2.getColumnIndex(MediaStore.Images.Media.LATITUDE));
Found out that there was not an error like I thought it would be. It was only a typing error.
put method has "Images.Media.LATITUDE"
update method has "Audio.Media.LATITUDE", changed this to "Images.Media.LATITUDE" like it is in the put method and it works now.
imageValues.put(MediaStore.Images.Media.LATITUDE, displayName);
getContentResolver().update(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
imageValues, selection3, null);

retrieve thumbnail image of a browser bookmark

I am creating my own android app widget to display browser bookmarks. & i need 2 show the thumbnail image of the bookmarks in my widget.
However the Browser.BookmarkColumns doesnt expose the "thumbnail" data.
any idea on how to get the thumbnail image ?
thanx in advance
It does expose the fav icon of the website. But its available in the database if that bookmark url was successfully visited at least once. Also when accessing the internal Android databases use the managedQuery() method.
Here is a snippet for accessing the fav icon from db.
Cursor mCur = managedQuery(android.provider.Browser.BOOKMARKS_URI,
projection, null, null, null);
mCur.moveToFirst();
int titleIdx = mCur.getColumnIndex(Browser.BookmarkColumns.TITLE);
int urlIdx = mCur.getColumnIndex(Browser.BookmarkColumns.URL);
int urlIdx = mCur.getColumnIndex(Browser.BookmarkColumns.FAVICON);

Categories

Resources