Use pre-populated databases with Realm - java

How could we convert SQLite database to a Realm database?
Is there any way to use pre-populated databases with Realm on Android?

Currently there is no way to automatically convert a SQLite database to a Realm database, you would have to manually read all data from the SQLite database and insert them into Realm.
An alternative could be the Realm browser that might make this easier, but it is currently available for MacOS X only. You can read more here: https://github.com/realm/realm-java/issues/435
For the second part: As Realm databases are just a single file so you can easily add a pre-populated realm database to you app and reference it with Realm.getInstance().

This is an answer to the second part of the question.
While shipping a prepopulated database in the assets folder of your app, analogous to the official Objective-C to bundle of Realm with your app, is still probably the fastest way to get a prepopulated Realm if you have a lot of data. But as of Realm Java 0.89 there is now an official way to prepopulate a Realm database on Android.
There is now a method that allows for specifying a transaction to be run when a Realm database is created for the first time. You should call this method, initialData(Realm.Transaction transaction), as part of setting up the RealmConfiguration Builder.
For example
RealmConfiguration config = new RealmConfiguration.Builder(context)
.name("myrealm.realm")
.initialData(new MyInitialDataRealmTransaction()),
.build();

Related

SQLite - Database minus list of values

I am working on project where I need to load data from local mobile DB(SQLite) to UI in meanwhile load data from server and when data download is finished insert the data from server to the database and update UI. I searching for way to download data from server and instead of delete all data in database and insert the new valid data just find the changes and "update" database. Is there some correct way to do this. I am working with SQLite database on Android phone.
SQLite isn't an online database meant to be ran on a server.
There aren't any free SQL Server options that I know of, but the NoSQL options include Firebase Realtime Database, Firebase Cloud Firestore, Realm Platform, Couchbase Mobile, Parse Server, etc...
Each of the listed options allow you to listen only to changes in the data. SQLite does not provide this option itself.

Can people access the Android realm file easily?

Can people hack the realm file to get the data in Android apps? I need to have a persistent object to save data to, so users can load it back when they restart.
RealmConfiguration realmConfiguration = new RealmConfiguration.Builder(this).build();
/* May need to take out the folioing line. */
Realm.deleteRealm(realmConfiguration);
Realm.setDefaultConfiguration(realmConfiguration);
realm = Realm.getDefaultInstance();
When the the RealmConfiguration is built through Builder(android.content.Context context) , the Realm file will be stored in /data/data/<packagename>/files. It is in the Android internal storage, and by default, only your app has the permission to access. See Saving files - Android doc. For a developer, there are some ways to get the file from the internal storage even he is not the owner of your app (eg.: root the phone :P).
You can choose to use Realm's encryption feature, which means even if others get the Realm file, it is still not possible to decode the data from the file. See Realm Encryption for more details.

update database of an app from another app in android

I need to update and insert rows in Sqlite from another app. I am working on an android app and I have to update database of another app,how can I do that,is it possible to access and edit another app's database from my app.
You should look at Content Providers. From the android official docs
Content providers manage access to a structured set of data. They encapsulate the data, and provide mechanisms for defining data security. Content providers are the standard interface that connects data in one process with code running in another process.
Android Docs explain how you can use it.
I understand what you are trying to achieve. correct me if i am wrong. You are developing android application in which you are trying to update your application database by fetching data from some different database. right? if yes, you have to get details of the database server /database connection details from the database owner if possible. in case if the owner of the database is not providing or not ready to give you credentials/database connection details then confirm whether they have any services/webAPI/web services through which they expose database data.
If they have webapi/webservices available then you can easily consume in your andriod application and do what ever operation you want to perform.
Hope this helps..

use existing sqlite database in java webapp

I have a java web app. I need to point to an existing sqlite database that I have in the project root (in eclipse).
I have all of the required drivers/jars.
The problem is that code to select from my sqlite database tables runs against a newly created database. I get an error that my table "Table1" doesn't exist. I believe this is because a new database is being created as opposed to using the existing sqlite database in the root with the tables and data that I need.
Does anyone know how I can force this local sqlite database to be used?
Per the SQLite docs, a new database will be created by default if it doesn't find an existing one -- so the most likely explanation is that the path in your connection string isn't pointing to your pre-existing database.
See the question here about relative paths in web apps: if your web-app is a servlet, use ServletContext.getResource to get the path to your database to use in the connection string. So, if your existing database is in /WEB-INF:
ServletContext.getResource("/WEB-INF/my.db");

How to update android database by connecting to a remote location

Im looking to connect to a URL and download a file (.db) and if it is of a higher version then update the database on the app. e.g. currently using Database1.db and this online one is Database2.db. However I'm not really sure how to go about this. Im using sqlite.
Thanks
From server end:
Instead of uploading database from url you can post xml or json data
which will give details bout database version and sql query
From android program
connect to url get the data check for the version if version from url
is higher than that of existing one then execute the sql query that
you got to create database and take backup of your data
Scenario
Lets consider one scenario you have some data in a table named y in
old database. but the new database which you are downloading doesn't
have that y table but it has some new table then what do you want to
perform with that data. Do you want to discard or store if store and
use then to where you want to store.

Categories

Resources