I want to insert required data for my application at the beginning and I will use these data. And I want to insert once, and there must be no duplicate. Therefore, in "onCreate", I'm doing like that : if the row count of table(such as student etc) is 0, I'm inserting students. I don't think it's the best way to do this. Therefore I want to learn if there is a better way.
If you want your database populated at install time and never any other time, your only reasonable option is to package your pre-populated database with your APK as a built-in resource. This has the advantage of simplifying your app.
Alternately, if you implement the SQLiteOpenHelper for your database, anything you insert during SQLiteOpenHelper.onCreate(SQLiteDatabase) will only ever be inserted either on the first run of you app or when someone clears all your app's data (which is more or less putting you back to a fresh install anyway). The SQLiteOpenHelper superclass knows whether or not to run the creation code when you call one of the getWritableDatabase() or getReadOnlyDatabase() methods to get your database reference.
It is worth noting that Android doesn't really let you run an installer the way desktop software does. If you need to do any setup work, you need to be able to detect and remember when your app has been run before.
Related
I have a requirement, if a record inserted in local DB(Sqlite) as soon as possible app triggers API call to send offline record to server. I don't want to use Timer and Alarm service because those will run continuously in background even there is no offline records in local DB. Any other alternative solutions?
Note: Target SDK level is Andorid 8 version.
As I understood your task (in general) - is how to invoke some function (some sendNewRecord() for instance) when new row(s) were inserted in some Sqlite table.
I suppose there are 3 alternative ways to implement such a task in Sqlite:
To invoke your function at the same place in code where you put your insert-new-data code. I think that's the most obvious and the simplest choice. I hope in your app there is only one place where you do it (Repository pattern or its analogue).
To put some observer(callback function) to your table and process changes in table. That more complicated task, and as far as I know you'll get this callback after every change in your table and it would be another task to identify what rows in your table were changed. Using modern Room framework and LiveData as a query result, you can observe changes in the table in more strait-forward way, but then nevertheless you have to detect inserted rows.
To use some scheduling mechanism for syncing (Executor, Timer and so on). You've wrote that it's not your case.
I would recommend you to choose first way. At least I don't see its disadvantages.
Althoung I have found a couple interesting posts about this topic, none of them is related to this kind of case or similar. I am developing an app which uses GoogleMaps API to show a map and I would like to have a database in which will be used to store all my points of interests (locations of e.g. police stations, hospitals, total of 478 entires) which will be placed on the map like markers aferwards.
These values will be inserted only once when the app is started for the first time, so I would guess that I do not need multiple threads or multiple instances of SQLiteHelpers in order to do this. Probably one of them should be enough to do the work, or not? Maybe it is important to mention that the users will not have a possibility to interact with the database.
I am having two activities so far, first is my InitActivity where I prepare some and check a couple of things important for the app and the second is my MainActivity. I would like to start with data insering in InitActivity as soon as the app starts but if it is possible not to wait for the whole process to ends in order to start the MainActivity, but to start it also when the data inserting starts. After the inserting finishs, I would like to call other method which will place the marker for each point of interest on the map. This method should be executed from the MainActivity. So I would need a background task which starts in one activity and informs other activity that the action is completed.
So, what could I use to carry out this kind of data inserting task and what would be the best way to do it (e.g AsyncTask - but is it possible to notify other activity that the process is completed)
Thx in advance
You are pretty much trying to invent the wheel here, which is wasting the efforts as this thing is already invented for long time. You most likely would be happy with tools like Android SQLiteAssetHelper or other similar helpers.
Android SQLiteAssetHelper
An Android helper class to manage database creation and version
management using an application's raw asset files.
This class provides developers with a simple way to ship their Android
app with an existing SQLite database (which may be pre-populated with
data) and to manage its initial creation and any upgrades required
with subsequent version releases.
It is implemented as an extension to SQLiteOpenHelper, providing an
efficient way for ContentProvider implementations to defer opening and
upgrading the database until first use.
Rather than implementing the onCreate() and onUpgrade() methods to
execute a bunch of SQL statements, developers simply include
appropriately named file assets in their project's assets directory.
These will include the initial SQLite database file for creation and
optionally any SQL upgrade scripts.
Firstly, what works is: A simple application that contains a sqliteDatabase. I populate this in the main activity and when the other activity is called, it queries the database and returns a string array. I put this result into a spinner using 'new ArrayAdapter'.
I don't think the code matters in this case as it works on the emulator fine. The spinner is populated ok on the emulator but won't populate on the phone?
Maybe the database doesn't even get created on the phone?
Anyone know what could be the cause of the problem? Thanks!
Your suspicion that the database isn't created might be a hint (is it likely you have been developing solely using the emulator and the database has been built in previous builds, whereas the current build doesn't create a new one when it doesn't yet exist ?).
Try logging whether the "onCreate"-method of the SQLiteDataBase class is fired when the class is instantiated. Try logging the SQL results for queries as they might indicate what is wrong (i.e. non-existing tables or column names, etc).
I am writing a backup application and need to know which records in the contacts database have been updated, so that I can backup only those records. I have looked at the documentation and it seems that there is a "DIRTY" constant field in ContactsContract.RawContacts class, which is supposed to be set to "1", for the rows that are updated. But it is not clear to me as to when will this field get cleared to "0". Can someone provide me example code on how to use this? Can this field be used to determine if a contact has been added or updated?
If this is not the correct way to achieve what I am trying to do can anyone suggest me another way. I am also aware that I can use the RegisterContentObserver() call to identify whenever there is a change in the Contacts database but this will require my application to be running always in the background, which is way too expensive and I do not want to do that.
If there is anyway to extract the timestamp when the various contacts have been added or updated that would be perfect too, but I cannot find how to do that.
Any help is very much appreciated.
I have looked at the documentation and it seems that there is a "DIRTY" constant field in ContactsContract.RawContacts class, which is supposed to be set to "1", for the rows that are updated. But it is not clear to me as to when will this field get cleared to "0".
In my experience, whenever there is a 'dirty' indicator of some sort, it is the responsibility of the backup/sync app to reset it once the data has been successfully committed during a backup/sync operation.
This can cause problems, however, when more than one application is used - the first one run at any time after data has been updated will reset the flag and the next one run wont find anything to backup/sync.
In this case if you require that a user is able to use a 'sync' app (for example) but you also want to have a 'backup' operation then registering a ContentObserver would serve a better purpose and there's no reason why this should be 'expensive' on resources if implemented correctly.
EDIT: Although there is no 'timestamp' there is a 'VERSION' field which is updated (which is when 'DIRTY' is set). If you backup this field, you could simply leave the 'DIRTY' flag set and compare current VERSION in the contacts DB with your most recent backup.
I am trying to adapt my application from a confirmation model to an undo model. For those of you who don't know, this is where you can delete something with one click but if it was a mistake you can undo it just as easily, as opposed to interrupting the user every time he/she wants to do something to ask the annoying "Are you sure you want to...?" question via dialog.
My app is backed by the Android SQLite DB and I want to be able to undo a limited set of delete and update operations. Also, I only need to be able to undo one sequential change and the information does not have to stick arround for very long.
Everything I read on undo/redo says to use a command model to store the data. My question is how can I store the database changes in a lightweight restorable way?
The idea of the command pattern is, that every command knows how it can be undone. For instance, an AddPersonCommand would add a new record to the Persons-table in your database. To undo this command, you would have to delete that person again.
Depending on the type of application and the complexity of the database, you can just write the changes to the database like you normally would. You always keep the last X command objects (X being the number of actions that can be undone), and if necessary, invoke their undo-method.
You can also use RestorableSQLiteDatabase library which is a wrapper around android's SQLiteDatabase and automatically generates restoring SQL queries to undo changes using a tag name.