为什么getReadableDatabase返回null?

时间:2013-12-09 01:33:14

标签: android sqlite android-sqlite

我正在使用SQLite数据库制作一个“选择你自己的结局”的故事,但是我遇到了一个错误,它说我的数据库是空的或者在尝试打开连接时是什么。
任何人都可以告诉我为什么会这样吗?

主要活动:

public class StoryActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        if (savedInstanceState != null) {
            story = savedInstanceState.getParcelable("story");
        } else {
            story = new Story(this);
            story.getStory(1);
        }
    }

    @Override
    public void onSaveInstanceState(Bundle state) {
        state.putParcelable("story", story);
    }
}

故事类:

public class Story implements Parcelable {
    private final String DATABASE_NAME = "story";
    private final int DATABASE_VERSION = 1;

    private int id, destinationA, destinationB;
    private String txtStory, decisionA, decisionB;
    private Cursor cursor;

    private SQLiteDatabase db;
    private SQLiteOpenHelper dbHelper;
    private Context context;

    public Story(Context con) {
        id = -1;
        txtStory = "";
        decisionA = "";
        decisionB = "";
        destinationA = -1;
        destinationB = -1;

        cursor = null;
        db = null;
        dbHelper = null;

        bundleDB(con);
    }

    private void bundleDB(Context con) {
        try {
            String destPath = "/data/data/" + con.getPackageName() + "/databases/" + DATABASE_NAME;
            File f = new File(destPath);
            if (!f.exists()) {
                Log.d("TEST","Copying database");
                File directory = new File("/data/data/" + con.getPackageName() + "/databases");
                directory.mkdir();
                copyDB(con.getAssets().open(DATABASE_NAME), new FileOutputStream(destPath));
            }
        } catch (FileNotFoundException e) {
            Log.d("TEST", "FileNotFoundException: " + e.toString());
        } catch (IOException e) {
            Log.d("TEST", "IOException: " + e.toString());
        }
    }

    private void copyDB(InputStream i, OutputStream o) throws IOException {
        byte[] buffer = new byte[1024];
        int length;
        length = i.read(buffer);

        while (length > 0) {
            o.write(buffer, 0, length);
            length = i.read(buffer);
        }

        i.close();
        o.close();

        Log.d("TEST", "Database has been bundled with app");
    }

    public Boolean getStory(int i) {
        try {
            open();

            cursor = null;
            if (db == null) return false;
            cursor = db.rawQuery("SELECT * FROM tblStory WHERE id = ?", new String[]{Integer.toString(i)});
            if (cursor.getCount() == 0) return false;
            cursor.moveToFirst();

            id = cursor.getInt(0);
            txtStory = cursor.getString(1);
            decisionA = cursor.getString(2);
            decisionB = cursor.getString(3);
            destinationA = cursor.getInt(4);
            destinationB = cursor.getInt(5);

            close();
        } catch (Exception e) {
            Log.d("TEST", "getStory exception: " + e.getMessage());
        }
        return true;
    }

    public void open() {
        dbHelper = new DBHelper(context);
        db = dbHelper.getReadableDatabase(); // This is the line that is bugged
    }

    public void close() {
        db.close();
    }

    // Inner class for use via open() method
    private class DBHelper extends SQLiteOpenHelper {
        public DBHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
        @Override
        public void onCreate(SQLiteDatabase db) {
        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        }
    }

    // Parcelable methods
    @Override
    public int describeContents() {
        return 0;
    }
    @Override
    public void writeToParcel(Parcel dest, int flags) {
    }

}

数据库(位于资源文件夹中):

tblStory (table)
    id (integer primary key)
    story (text)
    decisionA (text)
    decisionB (text)
    destinationA (numeric)
    destinationB (numeric)

logcat的:

Copying database
Database has been bundled with app
getStory exception: null

1 个答案:

答案 0 :(得分:3)

您可能忘记在Story中设置上下文。

添加

this.context = con;

在你的Story的构造函数中。