简体   繁体   中英

android sqliteDatabase table recreation

I have created one database and one table user in that while running my android app.

Problem is when at the first time i created user table, i miss the field password to create. So i rewrite my code so as to create the user table again which contains the field password in it.

But the problem is i had put the code in onCreate method inside my DatabaseHelper class.

Since the database is already exists, the function onCreate will never call.

Can anyone tell how can i solve this issue.?

You can use onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) method DatabaseHelper class

db - The database.

oldVersion - The old database version.

newVersion - The new database version.

Called when the database needs to be upgraded. The implementation should use this method to drop tables, add tables, or do anything else it needs to upgrade to the new schema version. The SQLite ALTER TABLE documentation can be found here . If you add new columns you can use ALTER TABLE to insert them into a live table. If you rename or remove columns you can use ALTER TABLE to rename the old table, then create the new table and then populate the new table with the contents of the old table. Parameters

few possibilities are available:

  1. if you have root access on your device or using an emulator - you can use adb shell to find your SQLite database and simply drop it (so it will re-created on next time your app starts) or using sqlite command line tool - modify the table structure yourself. Of course this fits only if you missed something during development and willing quick-fix it fast, not thinking about any existing users.

  2. increase the version number of your application and implement onUpgrade method, where you can do all required transformations on already existing database. This should be used for real application version upgrades, thinking about existing users and their application installations.

An easy option is just to uninstall the application on the device in the settings menu (or use the adb uninstall command), then the database will also be deleted and recreated if you run the application again.

Quick and simple without having to implement onUpgrade or something, that would be for when the application has hit the market/is really in use.

you can simply access your sqlite db using adb shell, you will need root access if your app is installed on the phone. Here is some reference - look for Examining sqlite3 Databases from a Remote Shell

Other way is to remove you database from Settings->Applications->Manage Applications->Your App and hit the Clear Data Button, that will remove your Sqlite db AND your Shared Preferences. After this you can rerun your app again using the correct table DDL and your Database will be created with the correct definition.

As programmatically speaking you will need to override the onOpen from your helper and put your table alteration DDL using db.execSql(). Put your alter table there or alternatively drop your table first and create the new one with the correct definition. Here is the class' reference SQLiteOpenHelper and SQLiteDatabase and please also refer to here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM