Bradly Spicer
Bradly Spicer

Reputation: 2318

How to I package my database in with my app?

I have an SQLite database which is required in my application. When I run it on my pc in an emulator it works flawlessly. When I run it on my phone it crashes because the method returns null as it can't connect to the database.

enter image description here

This is where I've placed my database in preparation. I'm unsure of how I'm meant to package this in with my application however.

Here is my DatabaseHandler.java:

package com.example.brad.myapplication;

import android.accounts.Account;
import android.database.DatabaseUtils;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;

import static android.database.DatabaseUtils.dumpCursorToString;

/**
 * Created by Brad on 19/07/2014.
 */
public class DatabaseHandler extends SQLiteOpenHelper {

   private static final int DATABASE_VERSION = 1;
   private static final String DATABASE_NAME = "carsGrid",
    TABLE_CARS = "cars ",
    KEY_ID = "id",
    KEY_POSTCODE = "postcode",
    KEY_ADDRESS = "address",
    KEY_IMAGE = "image";

    public DatabaseHandler(Context context) {
       super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE " + TABLE_CARS + "(" + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_ADDRESS + " TEXT,"+  KEY_POSTCODE + " TEXT," + KEY_IMAGE  + " TEXT)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CARS);

        onCreate(db);
    }

    public void createCar(Car car) {
        SQLiteDatabase db = getWritableDatabase();

        ContentValues values = new ContentValues();

        values.put(KEY_ADDRESS, car.get_address());
        values.put(KEY_POSTCODE, car.get_postcode());
        values.put(KEY_IMAGE, car.get_image());

        db.insert(TABLE_CARS, null, values);
        db.close();
    }

    public Car getCar(int id) {
        SQLiteDatabase db = getReadableDatabase();

        Cursor cursor = db.query(TABLE_CARS, new String[] {KEY_ID, KEY_IMAGE, KEY_ADDRESS, KEY_POSTCODE}, KEY_ID + "=?", new String[] { String.valueOf(id)}, null,null,null,null);

        if (cursor != null)
            cursor.moveToFirst();

        Car car = new Car(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2),cursor.getString(3));
        return car;
    }

    public int getCarCount() {
        //SELECT * FROM CARS
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_CARS, null);
        cursor.close();

        return cursor.getCount();
    }

    public String getRandomImageKey() {
        //SELECT * FROM CARS
        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.rawQuery("SELECT " + KEY_ID + "," + KEY_IMAGE + " FROM " + TABLE_CARS + " ORDER BY RANDOM() LIMIT 1", null);
        int columnIndex = cursor.getColumnIndex(KEY_ID);
        String toReturn = cursor.getString(columnIndex);
        return toReturn;
    }

    public Car getCurrentCar() {

        SQLiteDatabase db       =   getWritableDatabase();
        String sql              = "SELECT " + KEY_ID + "," + KEY_ADDRESS + "," + KEY_POSTCODE + "," + KEY_IMAGE + " FROM " + TABLE_CARS + " ORDER BY RANDOM() LIMIT 1";
        Cursor cursor           =   db.rawQuery(sql, new String[] {});
        Car car = null;

        try {
            if (cursor.moveToFirst()) {
                car = new Car(Integer.parseInt(cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3));
            }
        }
        finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
                db.close();
        }
        return car;
    }
}

Upvotes: 0

Views: 117

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006614

How to I package my database in with my app?

Use SQLiteAssetHelper to unpack your database from assets on the first run of your app.

Upvotes: 1

Related Questions