I would like to know if it is possible to have several DbOpenHelper in the same app Android but to use them to write and read in the same database?
because I'm trying to create tables from 2 different OpenHelper (with different names) and only the first one seems to create. when I try to run the 2nd one, i get an error...
Looks like You can't have more than one helper for each DB. Here's explained why: http://blog.foxxtrot.net/2009/01/a-sqliteopenhelper-is-not-a-sqlitetablehelper.html
Add the TABLE_CREATE statement to onOpen(SQLiteDatabase db) to ensure that the second table gets created in case the database connection is already opened. Further, the TABLE_CREATE statement should include IF NOT EXIST in case the table already exists.
Reference: http://jiahaoliuliu.wordpress.com/2011/09/26/sqlite-create-multiple-tables-with-different-sqliteopenhelper-in-the-same-database/
Related
I'm trying to rename a column in my Room database. I foolishly used the column name index and want to change it to id, but this migration function is causing headaches:
static final Migration MIGRATION_2_3 = new Migration(2, 3) {
#Override
public void migrate(#NonNull SupportSQLiteDatabase database) {
database.execSQL("ALTER TABLE items RENAME COLUMN index TO id;");
}
};
I followed this syntax diagram for the query:
Android Studio is giving me the error TO expected, got 'COLUMN', and I cannot use the database due to the RuntimeException:
Caused by: java.lang.IllegalStateException: Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number. You can simply fix this by increasing the version number.
The version number is correct, so I am assuming this issue is caused by the syntax problem above; I cannot find anything else wrong with my setup.
Android uses SQLite v3.19. That makes renaming a column using RENAME COLUMN not possible. The best approach is to recreate the table. – Leo Pelozo
Looks like I need to create function that drops the table so I can create a new one.
#Query("DELETE FROM items")
void dropTable();
... then create the table again, though I'm not sure how to go about this.
Update:
I was able (I think, we'll see...) to re-create the table by calling the above function, removing ALL migrations and setting the database version back to 1. Then I re-defined the database class itself with the proper names etc. and was able to insert data into it without any errors. Adding .fallbackToDestructiveMigration() to my database singleton class was also necessary.
Personally I think this is a little ridiculous just for simply re-naming a column; I was never able to simply rename the column and add a migration for the change, nor was I able to drop the table and re-create it with the proper column name and add that as a migration. But alas, this is Android after all.
I am working on a project on android where thousands of tables exist.Unfortunately I do not have fresh data base so what can I do to delete all record from all tables. i am using sqlite database .If it is possible please tell me.
If you want to remove the entire rows/data from table, then it's better to drop the table and recreate it. This is the usual convention and for this reason SqLiteOpenHelper has onCreate() and onUpgrade() method which creates table or upgrades it, when table is dropped, or database version is changed
For deleting/dropping table, the code fragment is
db.execSQL("DROP TABLE IF EXISTS " + YOUR_TABLE);
Check the first answer for reference What happens if an android app, which creates database, is then uninstalled?
I'm trying to write an android app that contains a database that would dynamically change its schema based on user input.
For example, suppose that you initially have a table in which the only column is a column for different breeds of puppies. This would be the primary key. The user can then dynamically add new attributes which would correspond to new columns in this table (e.g. color, has spots, size, etc.)
I was wondering whether the ALTER TABLE query must be executed in onUpgrade or whether I can do it in another method within the SQLiteOpenHelper subclass. I don't really know if it is really necessary to increment the database version every time the user wants to add a new attribute. Thanks!
I was wondering whether the ALTER TABLE query must be executed in onUpgrade or whether I can do it in another method within the SQLiteOpenHelper subclass.
You are welcome to execute ALTER TABLE statements whenever you want, though (as with all database I/O) on a background thread, please.
In your case, I do not know why you are using SQLiteOpenHelper, though. The point behind SQLiteOpenHelper is to help developers building apps with fixed (per version) schemas. That is not the route that you are taking, in which case SQLiteOpenHelper may not really be helping much.
onUpgrade will only be called when the database file already exists but the stored user version is lower than requested in constructor. I have used this function to carry out the changes I made to the database schema when I release a new version of the app. But if you want to add columns dynamically, yes you can use the ALTER TABLE command.
I'm trying to add a column to my database table which was created a while back and every time i try to run the app with the new column in the database adapter it crashes.
Why is this happening? I have changed the name of the database so it acts like a fresh table but this still doesn't work..
Please help????
If you are using a subclass of SQLiteOpenHelper as a lot of the Android examples suggest, you need to increment the DB_VERSION int that gets passed to the constructor. Otherwise the onUpgrade method doesn't get called and your db schema doesn't change.
How do i delete a whole table in android through code?
I'd say just use a DROP TABLE query?
(I'm asuming you're talking about a database, and you're using the SQLite that's in Android, right? http://www.sqlite.org/lang_droptable.html )
an example as requested:
DROP TABLE IF EXISTS mydatabase.myTable
You can delete a database easily by writing a test case inheriting from AndroidTestCase (You don't have to but its an option), that way you will get access to Context, and call deleteDatabase("mydatabase.db") on it.