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.
Related
I have an app published in the play store.
The app uses a database which holds a table which has a column of type int.
I'm doing a new change where I need to change the column type to long.
How do I go about handling it in the DatabaseHandler I'v created.
I want to preserve the data stored in the older apps database, so what should ideally be the code in the onUpgrade() function???
You don't need to change the database column type. An INTEGER column will happily contain all the bits needed to represent a Java long.
In fact, there's no long column type in sqlite.
I think using SQLite, the best way is to create a temporary table, copy all your table content, drop the old table and recreate the table with the right type on your column, then you can just copy the content from the temporary table and drop it...
I know this don't fell like the best approach, but I don't think SQLite have some alter table function.
As far I know you can t do this . But You can drop your table if it exists and create it again . Maybe you can find out some useful information here SQLite Modify Column or here Modify a Column's Type in sqlite3
I would like to know how to drop a database with ORMLite.
Is there already any API call?
Just dropping all the tables does not delete the whole database.
Thanks in advance.
Edit:
Looks like you figured it out. You do something like:
boolean success =
context.deleteDatabase(
"/data/data/source.package.goes.here/databases/database-name.db");
Edit:
Dropping a database is strange with ORMLite but I think it can be done. Really, when you do a dao.executeRaw(...) method, you have a connection open to the database engine that can perform just about any operation. You should be able to something like:
fooDao.executeRaw("drop database foo;");
That at least works for me under MySQL and it should under Sqlite.
Yes, ORMLite has the TableUtils class which allows you to create and drop tables. Here are the javadocs for the method.
You could do something like this,
TableUtils.dropTable(connectionSource, Model_Class.class, false);
for each table in the database, provided if you had model class for each table.
Reference:
http://ormlite.com/javadoc/ormlite-core/com/j256/ormlite/table/TableUtils.html#dropTable%28com.j256.ormlite.support.ConnectionSource,%20java.lang.Class,%20boolean%29
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.
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/