0

我有一些数据要写入数据库(三个字符串);我有一个私有类 DBHelper 扩展 SQLiteOpenHelper 并且我有dataBase = dbHelper.getWritableDatabase(); 当我执行 dataBase.insert() 时,我有一个 nullPointerException。

我在调试器中检查了我的数据以进行写入,它们不等于 null;

数据库助手:

private class DBHelper extends SQLiteOpenHelper { //for write and read data from DB

static final String GROUPPS = "groupps";
static final String IMPORTANCE = "importance";
static final String NAME_OF_TABLE = "Person";
static final String PERSON_KEY = "perskey";

    private static final String MY_QUERY = "CREATE TABLE " + NAME_OF_TABLE + " ( " +
            PERSON_KEY + " TEXT, " +
            GROUPPPS + " TEXT, " +
            IMPORTANCE + " TEXT);";

    public DBHelper(Context context) {
        // constructor of superclass
        super(context, "MyDBB", null, 2);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.d("", "--- onCreate database ---");
        db.execSQL(MY_QUERY);
    }

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

    }
}

我在数据库上写入数据的方法:

 public void writeDataToDB(ArrayList<Person> arrayList) {

    contentValues = new ContentValues();
    dbHelper = new DBHelper(activity.getApplicationContext());
    dataBase = dbHelper.getWritableDatabase();


    for (int i = 0; i < arrayList.size(); i++) {

        contentValues.put(PERSON_KEY, arrayList.get(i).getKey());
        contentValues.put(GROUPPPS, arrayList.get(i).getGroup());
        contentValues.put(IMPORTANCE, String.valueOf(arrayList.get(i).isImportant()));

        Log.e("writeDataToDB", "Start inserting " + contentValues.toString());
        long rowID = dataBase.insert(NAME_OF_TABLE, null, contentValues);
        Log.e("writeDataToDB - ", "Value inserted, rowID = " + rowID);
        contentValues.clear();
    }

}

在我的方法中,我在行有异常: long rowID = dataBase.insert(NAME_OF_TABLE, null, contentValues);
和 rowID = -1 这意味着 - 东西不对;

和我的 LogCat:

Error inserting groupp=Withoutgroup importance=false perskey=0r2-2B43414D272B4D16
    android.database.sqlite.SQLiteException: table Persons has no column named groupp: , while compiling: INSERT INTO Persons(groupp,importance,perskey) VALUES (?,?,?)
4

2 回答 2

1

Group是一个关键字,因此如果您的表将此作为列名,那么这可能是个问题。看

group=Without group
于 2012-05-03T11:03:36.767 回答
1

“重要性=假组=没有组 perskey=0r114343434343434343434343434343434343434343434343”

你必须把Without group放在单引号内,像这样:'Without group',否则SQLite会认为第二个词是SQL查询的一部分。

于 2012-05-03T10:57:15.150 回答