1

I'm attempting to create an application that checks the date, references the time the sun sets from a preloaded database, and outputs an alert 90 minutes before that happens. Less specifically (and probably more relevant to you), I'm trying to code an app that comes with a pre-filled database of information that it can access as needed. There are multiple tutorials for using SQLite in Android development, but they all assume that you're creating the database while the app is running and trying to enter user-determined data; the only tutorial I've found was from 2009, and there have been some updates since then that render that unusable (I think; regardless, it certainly wouldn't work in Android Studio). All other questions about this have just been marked as duplicates and referenced back to a very old question, answered with - you guessed it - that same outdated tutorial. This is the tutorial I'm talking about, if it helps at all; don't know that it will, but it might.

Edit: Why close it? I mentioned in my question that this question has technically been answered before, but it all links back to one outdated, irrelevant tutorial that doesn't help at all. The question may have been asked before, but the answer has been changed.

Jacob
  • 207
  • 1
  • 9

1 Answers1

1

Use SQLiteAssetHelper. You add one library, create a subclass of SQLiteAssetHelper (instead of the normal SQLiteOpenHelper), and put your database in assets/.

So, for example, in this sample app, I add the library in app/build.gradle:

compile 'com.readystatesoftware.sqliteasset:sqliteassethelper:2.0.1'

My DatabaseHelper extends SQLiteAssetHelper:

/***
  Copyright (c) 2008-2012 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  From _The Busy Coder's Guide to Android Development_
    https://commonsware.com/Android
*/

package com.commonsware.android.dbasset;

import android.content.Context;
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;

class DatabaseHelper extends SQLiteAssetHelper {
  static final String TITLE="title";
  static final String VALUE="value";
  static final String TABLE="constants";
  private static final String DATABASE_NAME="constants.db";

  public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
  }
}

And my database is in app/src/main/assets/databases/.

Everything else from there is just like normal use of SQLite in Android. Your database is automatically unpacked from assets on the first run of your app.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Well huh, that works quite well. Guess I should have looked outside the box a bit more. Quick clarification question because I'm a bit new to Android, where do I put the SQLiteAssetHelper file after downloading it and unzipping it? – Jacob Dec 20 '16 at 20:05
  • @Jacob: "where do I put the SQLiteAssetHelper file after downloading it and unzipping it?" -- I don't know what you mean. `SQLiteAssetHelper`, the Java class, comes from the library. You add the library to your module's `build.gradle` file, assuming that you are using Android Studio. If you downloaded the GitHub repo for `SQLiteAssetHelper`, while you can do that, it is unnecessary, unless you are forking the library. – CommonsWare Dec 20 '16 at 20:21
  • Oh. Duh. Never mind, I'm sick and therefore braindead. – Jacob Dec 20 '16 at 20:22