How to use OnUpgrade android - android

I have an app that uses sqlite to store some data. I want to add some data to that but don't want to add columns to table or change the table structure at all or add tables to db, I want just add some records to one table.
Is this a point to use OnUpgrade method or it didn't need to use this method for this record adding?
Is this adding some records to table become changing schema?

No, in any structured database adding records doesn't modify the db schema. You can add records like in a SQL databse, using INSERT statements. You can follow the d.android.com tutorial to storing data locally.

Related

Android Room fallbackToDestructiveMigration by keeping user's data

I have an Android app where I am using prepopulated database from assets.
I would like to know what is the best way to do migrations if I would like to keep user's data and also add any new records from prepopulated database if there are any. (I assume I need to compare records in device's database with prepopulated one)
I have tried fallbackToDestructiveMigration, but this method is just removing device's database and copying new one from assets. (So I am loosing user's data)
The standard migration approach is good, but I don't know how to check during migration if there are new records in prepopulated database and add them to the device's database after migration.
Thank you!
The fallbackToDestructiveMigration() method will destructively recreate database tables, ONLY IF the migrations steps that would migrate old database schemas to the latest schema version are not found.
Source: Room docs
In your case, lets say the pre-exisitng version of your DB is 1, and the new one you are going to provide is 2, then your migration steps from 1 to 2 can be as follows:
// Create a table for backup
CREATE TABLE myTable_backup(tableId VARCHAR,....);
// Copy old data to this backup table
INSERT INTO myTable_backup SELECT tableId,... FROM myTable;
// Delete old table
DROP TABLE myTable;
// Create the required table with new schema
CREATE TABLE myTable(tableId VARCHAR,....);
// Insert pre-existing data to your new schema
// Overwrite might / might not happen based on your Primary key strategy
INSERT INTO myTable SELECT tableId,... FROM myTable_backup;
// Delete the backup table
DROP TABLE myTable_backup;
The migration steps can be written as per this Room docs.

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

Read data from FTS table?

In one of implementing feature set application have to perform frequent Search Query directly from sqlite data Base. I found FTS could be better way to perform faster search. I created it but it's returning no data. I checked by opening db file, Virtual Table Created but no data there.
My question is, does Virtual Table required to insert data manually before to fetch any data. Any suggestion !!
The documentation says:
FTS tables are populated using INSERT, UPDATE and DELETE statements in the same way as ordinary SQLite tables are.

How to modify a table in a database sent (or ship) in the application without deleting the other tables?

I'm working on an Android application that use a DB sent in the assets folder. Periodically, I need to update one of the tables in the database, but without deleting the contents of the others (because they had data from the user).
I have read that increasing the version of DB will upgrade the entire DB, but that means resetting every single table.
Is there any way to achieve this?
You can handle it internally, I'm not sure what triggers your need to update the table, the initial setup of the app or later in your processing. The process is sort of rudimentary.
Let's say table A has data and you want to add 'col_new' to it.
DROP TABLE Temp
ALTER TABLE A RENAME TO Temp
Drop table A
Create table A with all the columns Table A has plus the 'col_new'.
Query table Temp and insert it into the A table something like this
insert into A (name, phonenumber) select name, address from Temp
Check that your counts match for both the tables. Set col_new as you may.
It's probably not what you wanted to hear, but I hope it helps.
I've finally done what CommonsWare recommended: to separate databases. I changed my design so right now I have two different databases (one containing the application information and the other containing the user information).
With this I'm able to update application DB without deleting user information.

Managing SQL tables in android

I have an app which requied more than one table.
The DB is mosly for read purposes and i want to know what is the best way to manage my tables.
I tought about 2 options.
Create new DB class with new DB for each table.
Create new table in the exits DB.
What is the best for better performance in reading?
hat is the best for better performance in reading?
For sure don't create new database but put all tables you need in one database. Reasons are more there, for instance now you don't know whether you will need sometime in a future to create some relations between these tables.
I's not "good" to have more db files which will represent one table, it's not comfortable and efficient as well. So my suggestion to you is to keep only one db file and put all tables in this one.
The best approach to manage SQLite database is to use SQLiteOpenHelper class that wraps all required logic for reading and writing from/to database. Then, SQLiteDatabase itself provides some API methods for inserting, updating and deleting from db.
At the end as my personal recommendation. If you'll have more than one table just how i mentioned create one SQLiteOpenHelper subclass for creating database and then for each table create object that will represent table "in objects" e.q. columns in table will become properties of object.
Finally for each table create DAO classes that will wrap CRUD operations and some specific methods for each table.
If you don't know how to start check these tutorials:
Android SQLite Database and ContentProvider - Tutorial
Android SQLite Database Tutorial
You can use SQLiteDatabase.
SQLiteDatabase has methods to create, delete, execute SQL commands, and perform other common database management tasks.
Database names must be unique within an application, not across all applications.
Read: Documentation

Categories

Resources