how to create database file(.sqlite) using Sqlite for Android? - android

i have created table using Sqlite3.and also inserted record in that table.its running successfully.but now i have created another table in same database..and written class for it.
now data is not inserted in another table though i have created table in same database and written respective code for it n XML as well.
i have given toast message after inserting record.it showing me record inserted successfully at position -1..and showing error like this "table is not exists or file encrypted."
and also if i tried to create table using Sqlite cmd then table is created but i am not getting where that file get stored???
can anyone tell me how to solve this problem.?
if you want code then will put here..
Thanks in Advance---

It is probably because your application is still working with the old database. If you check for an existing database before creating it, then your app would find the old one and use that. But the old database doesn't have the new table you added.
You should include some sort of versioning of your database and check that when you create your database. Then if the existing database is not up to the latest version, you can overwrite it.
A quick solution is to completely uninstall your application from the phone/emulator and then reinstall/run it, then the database will be up to date.

https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/
use this software for create table..
you can create multiple table, edit data ,operation can perform in this software..

Android uses provides an easy method of database access through custom SQLiteOpenHelper classes. This lets you give your current database a version number which you can increment every time you make a change to a schema.
When you access a database for the first time, it will call the onCreate() method and allow you to construct the database.
When you access the database in the future the SQLiteOpenHelper will check the version of the installed database against the one defined in the project and will call it's onUpgrade() method that provides the old version number. This means that you can provide custom SQL commands to update each legacy version of the database to the current version individually.
If you're just developing, it's easy to increment the number and provide a method like this one.
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS notes");
onCreate(db);
}
Note, this is taken from the Android SDK Sample Notepad Tutorial which covers this topic.

Related

does Android application come with sqlite databse created when an application is released?

I am working on some android application. I have two related questions about SQLite database in android.
1) I have got a bunch of SQL queries to create database tables in my SQLiteOpenHelper class. The queries are executed in onCreate(SQLiteDatabase db) method. Are the database and database tables re-created each time I run my application, using my cellphone? or Should I do something else to create database and tables at once if they don't exist?
2) When I release my application, like to Playstore, do I remove all the SQL queries and include the created database file in my application folder so that users can download the database along with the application? or should I leave the SQL queries to create database and tables so that users download my application and when they run it, the queries are executed and create the database?
Sorry guys for asking two questions at a time, but I believe they are quite related.
Thanks!
1)Once you have changed your database structure something like adding columns or modify existing table,
You should increase your database version number. Then only your changes will be applied.
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
For more info refer this link.
https://developer.android.com/training/basics/data-storage/databases.html
2) You can't remove all the SQL queries and include the created database file in your application. Because the table only created when the user runs the application. And it is created only once by calling the database initialization method. Only the database is updated if you increase the database version number.

how to upgrade database version with new rows with SQLiteAssetHelper

i have developed a tutorial app in which i have saved more than 500 questions and answers.
their is one more table called favourites. which is for user input.
now, i want to update my app with new questions and answers.
but i dont want to erase the data of favourites table (in case, user has marked some questions favourites, so those questions should not be erased from favourites)
so how can i do it?
because, i have used SQLassethelper library for database connectivity.
my old db contains:
data table(static table)
favourites table(local table)
so, according to sqliteassethelper documentation i added my new db:
that contains: updated data table. i didnt inserted favourites table here coz it will be created in script file.
and stored that db in assets>>databases folder.
then
i created a script fild
db.db_upgrade_1-2.sql
alter table "favourites" rename to "favourites_tmp";
create table "favourites" (
"id" Integer not null primary key autoincrement unique,
"question" text,
"answer" text,
"category" text,
"catid" integer
);
insert into "favourites" ("id","question","answer","category","catid") select from "favourites_tmp" "id","question","answer","category","catid" from "favourites_tmp";
drop table "favourites_tmp";
so i think here favourites table will be created with old data.
but
when i run the project, it says: no such tabld favourites.
The documentation tells you to
create a text file containing all required SQL commands to upgrade the database from its previous version to it's current version.
You can use any SQL commands, not only ALTER TABLE, but also INSERT.
The database file in the assets folder is used only if there is no old data (if the app is installed for the first time). If there is old data, SQLiteAssetHelper executes the SQL upgrade script instead.
The SQLiteAssetHelper project contains an example that shows how such a script would look like.
To keep the data in the favourites table, you do not need to do anything.
To add the new question/answers, use a bunch of INSERT statements.
(For how to get those INSERT statements, see How to compare two SQLite databases.)
finally i got answer for my questions.
as i said i am using SQLiteAssetHelper library.
and now i want to add new records and want to release the update version.
so i was finding the feature by which i can store new updated db in assets folder. and this library will update the old db in user's phone. with keeping particular table in old db.
but currently this library doesnt offers such feature.
the upgrade script can be use only to make changes is old db. we can add new updated db and copy the data from new one.
if we want to add new data and also dont want to erase local data table.
then we need to write insert queries in upgrade script as described in documentation of that library.
but if we have to add 100 queries then their is no shortcut for it.
we have to write 100 insert statements in upgrade script.
and in case if we gate a error saying
cannot upgrade read only database from version x to y then
here is the solution for it:
SQLITE cant upgrade read-only database from version 1 to 2

android - update just database content

I publish an android app in app store. After a while I do some edits special in database and move on to next version. My problem is edits and changes in database is just in content and no table nor column are added into it. so the schema is stick and the content is changed. Must I call onUgrade? Should I not change the database version?
If you use SqliteAssetHelper , You can do like the document says :
Upgrades via overwrite
If you have a read-only database or do not care about user data loss, you can force users onto the latest version of the SQLite database each time the version number is incremented (overwriting the local database with the one in the assets) by calling the setForcedUpgrade() method in your SQLiteAsstHelper subclass constructor.
You can additionally pass an argument that is the version number below which the upgrade will be forced.
Note that this will overwrite an existing local database and all data within it.
void onUpgrade (SQLiteDatabase db,
int oldVersion,
int newVersion)
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.
I would like to suggest don't increase database version then not need to call DB onUpgrade methods. If have changed in the schema you should need to call onUpgrade(). And upgrade database version also.

Is it possible to update some selected tables of SQLite DB when downloading an update of android application?

I am working on an android application that is using SQLite database to store data. I have "Favorite" table in database for storing favorite items of particular user.
The problem is when user will get an update of this application then I want to update database except "Favorite" table, because it will contain user specific favorite items.
Edit:
Now I know that I can update records on some tables in onUpgrade() event of SQLite database, but I really don't know how to manage two databases in an application? I have searched on Google but not get any idea to do this, there are many example to copy data from asset folder to new database.
I have to select all records from a table from "existed database" and copy to table of "new database". Table name may be same of both databases.
There are 2 aspects of updating Android app and DB:
1) If you just upgrade your app and don't change your DATABASE_VERSION (last parameter when you implement constructor of your class-descendant from SQLiteOpenHelper) - old database is retained and you may use it without any additional actions
2) If you change your DATABASE_VERSION- in your implementation of SQLiteOpenHelper there is method onUpgrade() which may look like this:
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// RUN YOUR SQL SCRIPTS:
db.execSQL(DBContract.Table1.SQL_DROP_TABLE);
...
// JUST DON'T DROP YOU FAVORITE TABLE HERE
// if required call onCreate():
onCreate(db);
}
So it's up to you whether to drop table or use it further.
More than that - you may decide what to do on the value of oldVersion and newVersion
Edit
If (according to your comments) you want to move data between databases you may look at this post
If you have copied your DB in app data or sd card thn it will remain even after updating data.*** if user is not clearing data manually from app settings
If no problem to download data from internet thn you by creating API
download data and update the data in required tables
Other if you dont wants to go with above one .. than create data file
for each table which you wants to update, keep it in assets folder
and after updating app when user starts app first time you can update
data except that fav table.eg Table wise json file with new data which you wants to update.
Hope I understand you question. and if not thn pls share it will try on tht

Android app update (previous tasks)

Suppose I have an app installed on my device that works on an SQLite database that is created alongside the first installation do the app. Now, suppose I make some changes on a new release that involve some changes on the DB schema. Obviously, it would be desirable to make the user know that it's DB will be wiped if the new release is installed. How can I make this advertisement prior to installation so the user can take needed steps in order to lose its data?.
In short, is there a way to open a dialog advertising the user on possible data loss and letting him decide what to do?
Thanks in advance,
Jose
Your database should have a method for updating the database
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// do something to upgrade
}
But that isn't called by default, but when your app first tries to open the database. At that point, you should be to architect a dialog that instructs your user and either do the upgrade or not.
It's also possible to alter the user's database within that onUpgrade method. For example, a string like this:
private static final String ALTER_TABLE3 = "ALTER TABLE exercises ADD COLUMN exscore REAL DEFAULT 0.0";
could be used in onUpgrade like so:
if (oldVersion == 5) {
db.execSQL(ALTER_TABLE3);
// now copy all of the values from integer to real
setUpMoveRoutine = true;
}
I did an upgrade from v4 to v5 that needed new elements within a table. And then called a method to move data as needed.
You should be able to manage just about any changes in your database
You shouldn't need to wipe the database to update the app, it's not a great user experience to do this and considering the SQLite implementation in Android is designed to allow you to upgrade the database it's also unnecessary.
Just implement the onUpgrade method in your SQLite helper class and provide upgrade steps to move between the different versions of your database.
The ALTER TABLE statements that SQLite supports are pretty limited, but you can add columns, if you need to make more complex structure changes you may need to copy data to a temporary table drop/create your table and then insert again.

Categories

Resources