Android的SQLite的getReadableDatabase [英] Android Sqlite getReadableDatabase

查看:196
本文介绍了Android的SQLite的getReadableDatabase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何机构可以帮我请。我在做的时间表应用程序,所以使用数据库。

Can any body help me please. I'm doing timetable application, so working with database.

我的程序崩溃的位置:

DatabaseHandler dbHelper = new DatabaseHandler(this.getApplicationContext());

                SQLiteDatabase db = dbHelper.getReadableDatabase(); //<---------------

                Cursor cur=null;
                 cur = dbHelper.fetchAll(db, "SELECT * FROM timetable");

                db.close();

我的数据库处理器类是在这里:

My DatabaseHandler class is here:

public DatabaseHandler (Context context) {

    super(context,DATABASE_NAME, null,DATABASE_VERSION);

}

//Creating Table
@Override
public void onCreate(SQLiteDatabase db){

    String CREATE_TABLE = "CREATE TABLE" + TABLE_NAME + "("
            + KEY_ID + "INTEGER PRIMARY KEY," + KEY_SUBJECT_NAME
            + "TEXT," + KEY_SUBJECT_CODE + "TEXT," + KEY_SUBJECT_VENUE
            + "TEXT," + KEY_TIME + "INTEGER," + KEY_DAY + "INTEGER" + ")";

    db.execSQL(CREATE_TABLE);
}

//Upgrading Database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){

    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);

    // Create tables again
    onCreate(db);

}

 /**
 * All CRUD(Create, Read, Update, Delete) Operations
 */

//Insert data
public void insertValues(int day, int time, String name, String code, String venue) {

    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();

    values.put(KEY_DAY, day);
    values.put(KEY_TIME, time);
    values.put(KEY_SUBJECT_NAME, name);
    values.put(KEY_SUBJECT_CODE, code);
    values.put(KEY_SUBJECT_VENUE, venue);

    db.insert(TABLE_NAME, null, values);

    //closing the database connection
    db.close();
}

public Cursor fetchAll(SQLiteDatabase db, String query){

    Cursor cursor = db.rawQuery(query, null);
      db.close();
    return cursor;

}

}

我会很乐意如果有人可以帮助我,因为这是我的毕业设计:)

I would be more than happy If someone could help me, cause this is my Final Year Project :)

推荐答案

最有可能你的上下文为空。尽量只使用这个而不是 getApplicationContext()

Most likely your Context is null. Try to use just this instead of getApplicationContext()

dbHelper = new DatabaseHandler(this);

注意:我建议这样做,而这样的:

Note: I recommend to do it rather like this:

public Cursor fetchAll(SQLiteDatabase db, String query) {
    Cursor cursor;
    db = this.getReadableDatabase();
    if (db != null) {
       cursor = db.rawQuery(query, null); // also rather use query method, ist more saf
    }
    return cursor;
}

如果你有方法,返回光标,你不应该叫 db.close()。如果您在活动调用它,你的光标将抛出错误是空的。

If you have method that returns Cursor, you shouldn't call db.close(). If you call it, your Cursor in Activity will thrown error that "is empty".

要松开光标等,我推荐使用的onPause()的onDestroy( ) 活动的方法,但也这取决于法的语境

To release Cursors etc, i recommend to use onPause() or onDestroy() methods of Activity but also it depends on Context of method.

因此​​,如果用于从数据库中添加,更新或删除数据,因此再在你的方​​法你的方法,我建议你使用尝试,终于块或的try-catch-最后,现在你应该叫 db.close()有你的解决方案清洁无面条code

So if your method is used for adding, updating or removing data from database so then in your method i recommend to you use try-finally block or try-catch-finally and now you should call db.close() to have you solution clean without spaghetti code.

于是小例子:

public boolean addRecord(String name, String pass, String value) {
    SQLiteDatabase db;
    try {
       db = this.getWritableDatabase();
       ContentValues data = new ContentValues();
       data.put(KEY_NAME, name);
       data.put(KEY_PASS, pass);
       data.put(KEY_VALUE, value);
       long result = db.insertOrThrow(TABLE_NAME, <nullColumnHack>, data);
       return result > 0;
    catch (SQLException ex) {
       Log.w("SQLException", ex.fillInStackTrace());
       return false;
    }
    finally {
       if (db != null) {
          db.close();
       }
    }
}

在这里你是光标 数据库并他们返回例如字符串列表&LT; T&GT; 或其他数据类型,因此也是在这里你应该叫的close()方法上的光标数据库有干净的解决方案。

Methods where you work with cursors, database and they return for example String, List<T> or other data-type so also here you should call close() method on cursors and database to have clean solution.

但我怎么会带来大约从数据库,不叫的close()方法返回在 DB

But how i meant about method that returns fetched data from database, do not call close() method on db.

希望它帮助同事。

这篇关于Android的SQLite的getReadableDatabase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆