Update Database Android

Can anyone tell me how to update the database in android. I created an app with an embedded database. I changed the version of the database in the manifest and created the on update method. I wanted to test it to see if the database was updating properly but when I use the adb command only the -r will allow me to do a reinstall but it keeps the database. Is there a way to run the adb command that will allow me to update the database. Thanks.


Asked by: Edgar859 | Posted: 24-01-2022






Answer 1

Here is how I would handle my updateRow method:

public void updateRow(long rowId, String code, String name) {
      ContentValues args = new ContentValues();
      args.put("code", code);
      args.put("name", name);
      db.update(DATABASE_TABLE, args, "_id=" + rowId, null);
}

Answered by: Audrey988 | Posted: 25-02-2022



Answer 2

What worked for me is changing the version number for the database. This is the constant number that is used by the onCreate() method of your content provider. Something similar works if you don't use a content provider for your database, and query it directly. See source code example here.

Answered by: Wilson564 | Posted: 25-02-2022



Answer 3

Have a look at the following resources and see if they're helpful:

Delicious Bookmarks
http://delicious.com/tag/android+adb
http://delicious.com/tag/android+database

DevX: Creating and Using Databases in Android
http://www.devx.com/wireless/Article/40842

Learning Android: Database Programming
http://learnandroid.blogspot.com/2008/01/android-database.html

Android Database Package Summary
http://developer.android.com/reference/android/database/package-summary.html

Answered by: Sophia194 | Posted: 25-02-2022



Answer 4

What worked for me is changing the version number parameter that I provide to the DBOpenHelper class which extends the SQLiteOpenHelper (the class used for creating/read/writing/etc... the Android database). When the version number parameter is incremented, the onUpdate() function is called once for the new value of this parameter.

Answered by: Melissa726 | Posted: 25-02-2022



Answer 5

If by updating you mean rebuilding (as I do during development when I want to start with a fresh database every time my application starts) then add this code before you access the database in your application:

this.getContext().getApplicationContext().deleteDatabase(DATABASENAME);

OR

Find the path to your database:

SQLiteHelper sql_database_helper = new SQLiteHelper(this.getContext().getApplicationContext());
File dbFile = getDatabasePath(DATABASENAME);
Log.v("XIX","DB FILE PATH:"+dbFile.getAbsolutePath());

Then delete your database from the command prompt:

> adb devices
List of devices attached
3246%$%^$&17   device  
> adb shell
$ su 
su
rm mydatapath

NOTE: You may not be able to access the su command without cracking your phone

Explanation: If you don't want to follow the advice given by r1k0 (which is probably the right way to do it if you are upgrading an already release app) then you can just delete the database with the following adb commands and a restart of your app will call the onCreate method again.

Answered by: Fiona172 | Posted: 25-02-2022



Answer 6

I don't advise to use this way:

public void updateRow(long rowId, String code, String name) {
      ContentValues args = new ContentValues();
      args.put("code", code);
      args.put("name", name);
      db.update(DATABASE_TABLE, args, "_id=" + rowId, null);
}

Your app will be vulnerable to sql injection attack, the best way to do that is something like this:

public void updateRow(long rowId, String code, String name) {
      ContentValues args = new ContentValues();
      args.put("code", code);
      args.put("name", name);
      String whereArgs[] = new String[1];
      whereArgs[0] = "" + rowId;
      db.update(DATABASE_TABLE, args, "_id= ?", whereArgs);
}

Answered by: John968 | Posted: 25-02-2022



Similar questions

android - Adding data to a database cursor

Maybe I'm going about this the wrong way, but if so, please correct me. Here is the situation: I have a query which returns URI strings for ringtones stored in a database. I am trying to add a "column" to this cursor with the ringer "Title" (since this can change outside my program). I can successfully use RingtoneManager to get the title, but I cannot figure out how to add this "column" to the...


android - Backup and restore SQLite database to sdcard

How can I backup my database to the sdcard automatically in my app? And afterward, how do I restore it?


Select first SMS on Android database inbox

I am desperate to find the solution so I ask for help! I am a new french programmer. My objective is to create a widget able to show SMS. My problem is that I don't know how create a cursor which select the first SMS in content://sms/inbox Excuse my bad English, I hope you will able to understand my wich. Thank you for your answer. this is my code: package sfeir.monwidget; import android.R.string; import an...


android - where is database file in the device

iam trying to locate the database files on my nexus device.. is there anyway of doing it without rooting the device? thanks ray.


how to get macid and mobile number only in android mobile device not in database

how to get macid and mobile number only in android mobile device not in database


android import export database

there. I am trying to implement a feature into my app to copy the current database unto the sdcard. Are there any tutorials or code snippets to achieve this. I've already looked at this site, http://mgmblog.com/2009/02/06/export-an-android-sqlite-db-to-an-xml-file-on-the-sd-card/ ... but I wont be able t...


Android database backups

I'm looking for ways to back up the database of my app on an Android phone. I know that SQLite databases are just files, so I'd expect to be able to just copy the file to SD-card if one is available. However, I'm unsure as to how I'd prepare my database/activity for backup/restore. When starting, my main activity reads the entries from one table in the database and displays them in a ListView. ...


android - How to use my own sqlite database?

I put my database field in "assets" folder. And use the code from this blog to copy the database to "/data/data/my_packname/databases/", (This copy code i run it in the onCreate() method when i run this app) then use select * from ... to get data. But it gives me the exception: no such table. Some...


android - Use DDMS to push the sqlite database file faild

i did not find my package name folder in the data/data. There just are many folder named com And then i try to push my sqlite database to data/data/com, But faild. Why? Pls help me . Can give me a smaple? How to use already exist sqlite database. I have learned from a blog :


android - Using my own SQLite Database

I have a sqlite database, and i put this file in "assets" folder. The code like below, Pls help and tell what's wrong in this code, How to use my own sqlite database. public class DataBaseHelper extends SQLiteOpenHelper { private static String DB_PATH = "/data/data/com.SGMalls/databases/"; private static String DB_NAME = "mallMapv2.sqlite"; private SQLiteDatabase myDataB...


android - Adding data to a database cursor

Maybe I'm going about this the wrong way, but if so, please correct me. Here is the situation: I have a query which returns URI strings for ringtones stored in a database. I am trying to add a "column" to this cursor with the ringer "Title" (since this can change outside my program). I can successfully use RingtoneManager to get the title, but I cannot figure out how to add this "column" to the...


android - static database class to use with any activity

I am new to Android, and I haven't developed any app regarding databases on Android yet. So I have a few basic questions. I am after a good database class sample, that will let me to run the CRUD operations. I would like to use it as a static class like: clsDB->Select("select * from clients"); or objClient->Delete(clientid); I am wondering if some...


android - Backup and restore SQLite database to sdcard

How can I backup my database to the sdcard automatically in my app? And afterward, how do I restore it?


Loading flat files into Android database table

In Android when you upgrade a database all data is lost. I'm going through a phase of development right now that is forcing many database upgrades. I don't want to lose all my data (and have to manually re-enter it) each time I upgrade my database. I would like to store my data in flat files and load those flat files into their respective tables each time a database upgrade occurs. Wha...


Select first SMS on Android database inbox

I am desperate to find the solution so I ask for help! I am a new french programmer. My objective is to create a widget able to show SMS. My problem is that I don't know how create a cursor which select the first SMS in content://sms/inbox Excuse my bad English, I hope you will able to understand my wich. Thank you for your answer. this is my code: package sfeir.monwidget; import android.R.string; import an...


android - where is database file in the device

iam trying to locate the database files on my nexus device.. is there anyway of doing it without rooting the device? thanks ray.


how to get macid and mobile number only in android mobile device not in database

how to get macid and mobile number only in android mobile device not in database


Correct path to sqlite database used by JAR imported into my Android app?

My Android app is using a database through an API packaged in an external JAR. Since the JAR is opening/updating/closing the SQLite database file, do I need to make sure it uses its own package name instead of my own? Im getting errors where it fails to open the database file and Im guessing its because the package name Im telling it to use is incorrect: should it be using the package name in the JAR file? Code tha...


android import export database

there. I am trying to implement a feature into my app to copy the current database unto the sdcard. Are there any tutorials or code snippets to achieve this. I've already looked at this site, http://mgmblog.com/2009/02/06/export-an-android-sqlite-db-to-an-xml-file-on-the-sd-card/ ... but I wont be able t...


Android database backups

I'm looking for ways to back up the database of my app on an Android phone. I know that SQLite databases are just files, so I'd expect to be able to just copy the file to SD-card if one is available. However, I'm unsure as to how I'd prepare my database/activity for backup/restore. When starting, my main activity reads the entries from one table in the database and displays them in a ListView. ...






Still can't find your answer? Check out these communities...



Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android



top