0
我有一個SQLite數據庫,在我的應用程序中是必需的。當我在模擬器上的PC上運行它時,它的工作原理完美無瑕。當我在手機上運行它時,它會崩潰,因爲該方法無法連接到數據庫,因此返回null。如何將我的數據庫打包到我的應用程序中?
這是我已經把我的數據庫做準備。但我不確定我是如何將它與我的應用程序打包在一起的。
這裏是我的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;
}
}
嗨@CommonsWare,感謝您的職位,我是否承擔超級(上下文,DATABASE_NAME,context.getExternalFilesDir(null).getAbsolutePath(),null,DATABASE_VERSION);進入MyActivity.java? –
@BradlySpicer:不,但其中的一個簡化版仍然存在於「SQLiteAssetHelper」的子類中:https://github.com/jgilfelt/android-sqlite-asset-helper#usage – CommonsWare
對不起,如果這變得乏味,它有一個小問題。 http://puu.sh/apPcu/f6ec1c110f.png –