Programatically update SQLite database in android? - android

I have around 2600 entries in my SQLite database. I need to delete all those entries and put in updated entries. I know that the database can be updated by changing the database version in the DatabaseHandler class, but that can only be done if the whole app is updated. I need to programatically update the database. Can this be done? I searched but couldn't find a satisfactory answer. Thanks.

I guess you can call a method some what like this
public void emptyDatabase() {
db = this.getWritableDatabase();
db.execSQL("DELETE FROM " + TABLE_Your_Tbl);
}
Even this will be able to delete those values quickly. Without updating the version number.
Plus as you mentioned that you have to delete the records and add new values.
It will be much faster that updating those values.

Related

Delete all data from all tables in Sugar ORM

I use Sugar ORM 1.5 in my app and I want to clear all info when the user logoff.
I want to avoid deleting the database file or use pure SQLite scripts if possible.
The only solution I found was using deleteAll on all tables, but I want to know if there is a better way to do it.
Regards,
EDIT
I solve the problem I had deleting the database just calling SugarContext.terminate(); before deleting the database and SugarContext.init(context); after.
Looks like it's the best solution like Henry Dang pointed in the comments, and it is faster than deleting all data.
This snippet works for me. It terminates the context deletes all tables and recreates them:
SugarContext.terminate();
SchemaGenerator schemaGenerator = new SchemaGenerator(getApplicationContext());
schemaGenerator.deleteTables(new SugarDb(getApplicationContext()).getDB());
SugarContext.init(getApplicationContext());
schemaGenerator.createDatabase(new SugarDb(getApplicationContext()).getDB());
SchemaGenerator is from com.orm, my SugarORM version is 1.5.
for other users who just want to delete all records (not tables or database)
public static void deleteAllrecords(Context applicationContext) {
List<Class> domainClasses = getDomainClasses(applicationContext);
for (Class domain : domainClasses) {
SugarRecord.deleteAll(domain);
}
}
if we want to delete sequence also you can add this line inside for loop
SugarRecord.executeQuery("DELETE FROM SQLITE_SEQUENCE WHERE NAME = '" + NamingHelper.toSQLName(domain) + "'");

How to Close SQLite Cursor and other open SQLite database?

I am trying to write a code in Android and going through following errors. I am opening a SQLite database and using Cursor.
Java Code
for(int y3=0;y3<10;y3++)
{
String sql_query5= "SELECT * FROM Data WHERE Id="+y3+"";
Cursor cur5 = SQLiteDatabase.openDatabase(db, null, SQLiteDatabase.OPEN_READWRITE).rawQuery(sql_query5, null);
if(cur5.moveToNext())
{
//
}}
So i need to display the Age in the layout but i am facing SQLiteCantOpenDatabaseException and Database object not closed. I don't understand the errors.
Please let me know some solution and suggestion !!!
Do not use hard-coded file paths. You have no guarantees about the structure of the filesystem.
Learn to use SQLiteOpenHelper. This is what helps you create and maintain your SQLite database. Instead of trying to open the database yourself, you create a new SQLiteOpenHelper and call getReadableDatabase().
When you are done reading from a Cursor, call cursor.close().
If you want to close the database, you can call close() on it as well; just be aware that any cursors you got by querying it will no longer be able to give you data, so make sure you are finished them those before closing the database.
add '' in your
String sql_query5= "SELECT * FROM Data WHERE Id='"+y3+"' ";

Update ListView based on SQLite backed ContentProvider

I'm a new Android Developer and seem to have gotten in a little over my head. I am trying to make a listView update when I add more content to the list.
The ListView is based off of a SQLite database. I was able to get the ListView to be based on the SQLite database by making a ContentProvider for the SQLite database (which was suggested here). Now my issue is that I want to update the SQLite database and have it reflected on the ListView. I am using a loader and according to this if I implemented the loader right it will monitor the data.
I tried updating the SQLite database directly but, that didn't cause the ListView to update without closing and reopening. My instinct from there is that I should implement the insert method in my contentProvider. I did a very simple implementation:
#Override
public Uri insert(Uri uri, ContentValues contentValues) {
return ContentUris.withAppendedId(uri, mCollectionDB.insertCollection(contentValues));
}
Unfortunately, the result of this is my app crashes with a "java.lang.NullPointerException". This is especially confusing as using the exact same contentValues to make the .insertCollection call from my MainActivity works without issue.
The issue I'm really interested in is how to get my listView to update when I insert data into my SQLite database. If inserting into the ContentProvider is irrelevant then please ignore that. I'm not really sure where I went wrong so I'm not sure what other code may be useful, but I'm happy to edit in more code if it'll help.
You can check loader concept.
You can start with
http://www.vogella.com/articles/AndroidSQLite/article.html#todo
Following are other 2 good tutorials
http://mobile.tutsplus.com/tutorials/android/android-sdk_content-providers/
http://mobile.tutsplus.com/tutorials/android/android-sdk_loading-data_cursorloader/
Hey here is one example to insert data into sqlite database and display it in list view.
Have a look at it
The idea is simple insert data into database and on click of view button initialize List view with arraylist that contains data already inserted.
You can ask if you have any further queries.

Inserting and updating values in sqlite database in android

I created database for inserting and updating values which are came from server this is ok.
My problem is when updating my values if new values are came from server how can i mange both inserting and updating at a time. Please provide any suggestions to me. Thanks in Advance.
Your question is pretty broad however, the values that are coming across from the server need to have a unquie identifier if you are wanting to overwrite them later with new values. If you get values coming to you and want to overwrite them later you need to know which ones to overwrite hence you could go straight there with a unique identifier.
This is code for inserting values into database
public void insert(ContentValues values)
{
DataBase_Object.insert(TABLE_Name , null, values);
}
This is code for Update values into database
public void putQty(ContentValues values)
{
DataBase_Object.update(TABLE_Name, values ,
Where Condition, null);
}
Hope it helps.
I believe that you are asking how to insert new rows or overwrite your existing rows in one step. While that is possible in a single raw SQL as discussed in this answer, I found that it easier to do this in two steps in Android and preserving values in all your columns.
I posted my answer here.

How can I recreate a database with default values?

In my application, I want to delete my existing database and create a new one with default values. Default values can be inserted to the database from XML.
Does anyone have any idea on how to reuse a database?
Assuming that you are using a SQLite database, to delete all the rows from all of your tables inside your database you can use db.execSQL() and heed the advice from this question Drop all tables command:
You can do it with the following DANGEROUS commands:
PRAGMA writable_schema = 1;
delete from sqlite_master where type = 'table';
PRAGMA writable_schema = 0;
you then want to recover the deleted space with
VACUUM
and a good test to make sure everything is ok
PRAGMA INTEGRITY_CHECK;
If you haven't written a way to read your XML data yet, this is excellent reading: Store parsed xml data to sqlite ? Android
Well basically that's not an Android specific question.
Firstly when do you want to recreate the database with default values and how to trigget it.
In an UI event like button click etc?
Or when you start/stop or destroy your activity?
In any cases you need to drop the database and recreate the whole structure (tables,relationships etc.) again.

Categories

Resources