IllegalStateException:在DatabaseHelper文件中

时间:2016-01-06 04:19:36

标签: android android-sqlite

以下链接包含我获取例外的文件的gist。我在第29和66行得到例外。

https://gist.github.com/ShashwathKumar/e1b26e8d4f6956c2afd4

2 个答案:

答案 0 :(得分:0)

在这两个地方使用try-catch

public void insertContact(contacts c){
    try{
       db = this.getWritableDatabase();
       ContentValues Values = new ContentValues();

       String query = "SELECT * FROM CONTACTS";
       Cursor cursor = db.rawQuery(query, null);
       int count = cursor.getCount();

       Values.put(COLUMN_ID, count);
       Values.put(COLUMN_NAME, c.getName());
       Values.put(COLUMN_EMAIL, c.getEmail());
       Values.put(COLUMN_PASSWORD, c.getPassword());

       db.insert(TABLE_NAME, null, Values);
       db.close();
    }catch(Exception e){
       System.err.println(e);
    }
}

也可以使用try-catch作为其他方法。

答案 1 :(得分:0)

编写您的创建表查询

public static final String query_create_table = "CREATE TABLE IF NOT EXISTS "
        + DatabaseHelper.TABLENAME
        + "("
        + DatabaseHelper.COLUMN_ID
        + " integer primary key autoincrement ,"
        + DatabaseHelper.COLUMN_NAME
        + " text not null ,"
        + DatabaseHelper.COLUMN_EMAIL
        + " text not null ,"
        + DatabaseHelper.COLUMN_PASSWORD + " text not null" + ");";