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.
Related
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.
From what I understand, once a SQL db is created and populated it remains on the device until the app is uninstalled. For performance reasons I don't want to re insert all my rows every time the app is lauched, how can I wrap my insert statements so that I populated the db only the first time the app is launched?
Right now I have the inserts in the onCreate() method of my main activity. What is the best practice?
Thanks
Use SQLiteOpenHelper to manage your databases. Put your database creation and population code in the onCreate() of the helper - it gets invoked exactly once when the database file did not exist.
before you could insert check for no of rows in the table.
if(rows<1)
{
insertValues
}
else{
//Do nothing //
}
you can get no of rows by using
int numRows = (int) DatabaseUtils.queryNumEntries(db,table_name);
I have a pre-established SQLite Database in my application. It has a table with rows about 20 rows of text. I want to be able to add additional rows to the table without deleting all of the previous information. The only way I have seen which would allow me to do this is to delete all of the previous databases and then recreate it with the new rows. There must be a better way. Thanks for your help!
Are you confusing rows with columns?
If you really do mean rows then as antlersoft points out, using the SQL INSERT INTO statement will simply add a new row to a table without affecting any existing table data. This is one of the most basic and commonly used SQL statements.
If you actually mean you need to add columns then use the SQL ALTER TABLE statement.
See..
SQL INSERT INTO statement
SQL ALTER TABLE statement
The Android framework, as it relates to SQLite (using a SQLiteOpenHelper) provides two distinct methods for handling database lifecycles - onCreate(), used when the database needs to be created from scratch, and onUpgrade(<database>, int oldVersion, int newVersion) for handling updates. You can specify the "new" version number in the constructor for the superclass of your SQLiteOpenHelper, and the framework knows to call onUpgrade() based on this parameter and the internal version # in the actual sqlite database.
So, to modify your database during a version change just override onUpgrade() and run whatever SQLite stuff that you need.
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/