如何用我的应用程序打包我的数据库?

时间:2014-07-24 23:49:44

标签: java android sqlite

我的应用程序需要一个SQLite数据库。当我在我的电脑上在模拟器中运行它时,它可以完美地工作。当我在手机上运行它时崩溃,因为该方法返回null,因为它无法连接到数据库。

enter image description here

这是我准备好数据库的地方。我不确定我是如何用我的应用程序打包的。

这是我的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;
    }
}

1 个答案:

答案 0 :(得分:1)

  

如何将数据库打包到我的应用程序中?

Use SQLiteAssetHelper在第一次运行应用时从资产中解压缩数据库。