Delete all record from the data base - android

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?

Related

Do you have to alter the database schema in the onUpgrade method of the SQLiteOpenHelper?

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.

Android upgrading a DB with a NOT NULL column - what happens to old users

I have an app released in app store
I want to add a new column to the user table in the sqlite db and want it to be not null
But I also want old users to be able to use the app
What will happen if an old user updates their app with the new version? At login, I get the info from the server and I insert it in the db. The db insertion will probably stop when there is no value for the new column
Also, how do I do the upgrade itself?
in onUpgrade I do "ALTER table USER..."
in the constructor of the SQLite helper I add the new DB version
what else?
Also, I've added 3-4 totally new tables needed for new features. I've called their create queries in the onCreate method of the SQLite helper. Should I do anything else in addition to this?
in the constructor of the SQLite helper I add the new DB version
That will trigger onUpgrade(), good.
However, you cannot use ALTER TABLE to add a column with NOT NULL.
Here's what you can do in onUpgrade() to preserve user data:
Rename the old table to a temporary name
Recreate the table with the new column and NOT NULL
Populate the new table from the old temp table and supply the new column a reasonable non-null default value
Drop the temporary table
Can you give me an example lets say table is called USER with fields NAME and EMAIL and now I want to add a new field AGE?
Here's an example:
sqlite> create table user(name, email);
sqlite> insert into user select 'foo','bar';
sqlite> alter table user rename to user_temp;
sqlite> create table user(name, email, age not null);
sqlite> insert into user select name,email,-1 from user_temp;
sqlite> drop table user_temp;
sqlite> select * from user;
name|email|age
foo|bar|-1
Also, I've added 3-4 totally new tables needed for new features. I've called their create queries in the onCreate method of the SQLite helper. Should I do anything else in addition to this?
should I put the new creates for the new tables in both onCreate and onUpgrade or only in onUpgrade?
Make sure the same new tables are created in onUpgrade().
onCreate() is only run when the database is created for the first time, not on upgrade.
After both onCreate() and onUpgrade() the database schema (table structure) should be compatible. How you implement it is up to you. Putting the CREATE TABLEs there in onUpgrade() is an option. Some people prefer to call onCreate() insinde onUpgrade(), which can cause some headache when trying to migrate old data.

SQLIte DROP table ROLL back

In Android SQLite I had one table MyTable. By mistake I dropped it after upgrade the DB.
How can I ROLL BACK that dropped table if it's possible.
Any good answer will be accepted.
Thanks.
Dropping tables is not a recoverable action, unless performed as part of a transaction that is rolled back (which appears to be not the scenario for your particular case).
From the SQLite documentation:
The DROP TABLE statement removes a table added with the CREATE TABLE statement. The name specified is the table name.
The dropped table is completely removed from the database schema and the disk file. The table can not be recovered. All indices and triggers associated with the table are also deleted.
That's not quite the complete picture, as the behaviour under rolled-back transaction can be seen with (tested on https://sqliteonline.com/):
drop table if exists paxtable;
create table paxtable (paxcolumn integer);
insert into paxtable values (42);
begin transaction;
drop table paxtable;
rollback;
select paxcolumn from paxtable;
That shows that the table still exists after the rollback. If you commit rather than roll back (or if you remove the transactional control altogether), the table has died, expired, gone to meet its maker, shuffled off this mortal coil, <insert your favourite metaphor here>.
So, since you didn't do it as part of a rolled-back transaction (as evidenced by the fact the table has actually gone), you'll need to re-create it from scratch (or from backups if possible).
CLARIFICATION:
Although you can commit or rollback DML statements like "insert" or "delete" (provided you do it within a transaction), in general you cannot rollback a DDL statement like "alter table" or "drop table".
This is true for most databases under most circumstances: Oracle, MSSQL, mySQL, etc.
There is an exception for sqlite: if you drop table in a transaction, then a rollback will restore that table.
Otherwise (per the sqlite manual):
http://sqlite.org/lang_droptable.html
The DROP TABLE statement removes a table added with the CREATE TABLE
statement. The name specified is the table name. The dropped table is
completely removed from the database schema and the disk file. The
table can not be recovered. All indices and triggers associated with
the table are also deleted.
PS:
This link discusses "DDL", "DML" and related acronyms, if you're interested:
http://www.orafaq.com/faq/what_are_the_difference_between_ddl_dml_and_dcl_commands
I think there's two different interpretations of this question and I want to make sure both get answered and demonstrated conclusively since this is still the top search result for sqlite drop table rollback (and the links to the SQLite documentation seems misleading as well).
To the first question, you can rollback a drop table DDL action that occurs within a transaction scope, i.e.,
// with connection created
var transaction = connection.BeginTransaction();
try {
// create a table
// drop a different table
transaction.Commit(); // go ahead and commit if everything is successfully
}
catch
{
transaction.Rollback(); // rollback in case of error
}
And to confirm this is the same behavior in a language-agnostic fashion, here's the same behavior using the SQ Lite command-line shell:
sqlite3 demo
// outside transaction scope
create table tbl1( col varchar(10));
// list the current tables
.tables
// start a transaction that wraps both your DDL commands
begin transaction;
enter code here
create table tbl2 (col varchar(10));
drop table tbl1;
rollback;
.tables
The expectation is that the final list tables command, should still return tbl1 since both the create table and drop table commands were both rolled back. Another way to say this is that SQLite is not bound by the same DML/DDL distinction for what operations can be rolled back that are present in Oracle.
For the second interpretation of the question, i.e., can I recover a table dropped outside of a transaction scope (which would also entail the "Oh S#%T" experience you may have had as a developer as well as disaster recovery), the references to the SQ Lite documentation are appropriate:
The dropped table is completely removed from the database schema and the disk file. The table can not be recovered. All indices and triggers associated with the table are also deleted.

Android SQLite Database Update

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.

Android SQlite database table, adding column

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.

Categories

Resources