为什么没有插入SQLite数据?

时间:2017-06-27 22:48:14

标签: android android-sqlite

我正在制作应用,我正在添加一个复活节彩蛋。我试图这样做,以便当用户找到复活节彩蛋时,会在SQLite列中插入一个整数。该值未插入列中。

public boolean easterEggWasFound(int wasFound) {
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_6, wasFound);
    guessTheNumber.insert(TABLE_NAME, null, contentValues);
    return true;
}

整个SQLite Helper类:

public class SQLDatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "guessthenumber.db";
public static final String TABLE_NAME = "guess_the_number_table";
public static final String COL_1 = "NAME";
public static final String COL_2 = "TRIES";
public static final String COL_3 = "DIFFICULTY";
public static final String COL_4 = "DEFAULT_NAME";
public static final String COL_5 = "SUCCESS";
public static final String COL_6 = "EASTER_EGG";
SQLiteDatabase guessTheNumber = this.getWritableDatabase();


public SQLDatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);
}

@Override
public void onCreate(SQLiteDatabase guessTheNumber) {
    guessTheNumber.execSQL("create table " + TABLE_NAME + " (NAME TEXT, TRIES INTEGER, DIFFICULTY TEXT, DEFAULT_NAME TEXT, SUCCESS INTEGER, EASTER_EGG INTEGER)");
}

@Override
public void onUpgrade(SQLiteDatabase guessTheNumber, int i, int i1) {
    guessTheNumber.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
    onCreate(guessTheNumber);
}

public boolean insertData(String name, int tries, String difficulty, int success) {

    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_1, name);
    contentValues.put(COL_2, tries);
    contentValues.put(COL_3, difficulty);
    contentValues.put(COL_5, success);
    long result = guessTheNumber.insert(TABLE_NAME, null, contentValues);
    return result != -1;

}

public Cursor getAllData() {
    String[] columns = {COL_1, COL_2, COL_3, COL_5};
    return guessTheNumber.query(TABLE_NAME, columns, null, null, null, null, null);
}
public void removeAll() {
    SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
    sqLiteDatabase.execSQL("delete from " + TABLE_NAME);
}


public boolean updateDefaultNameData(String defaultName) {
    ContentValues contentValues = new ContentValues();
    //I have to delete the previous number because in the method, it
    //chooses only the first name in the row
    //This should never contain more than one name
    contentValues.put(COL_4, defaultName);
    guessTheNumber.update(TABLE_NAME, contentValues, null, null);
    //guessTheNumber.insert(COL_4, null, contentValues);
    return true;
}

public Cursor getDefaultNameData() {
    String[] columns = {COL_4};
    return guessTheNumber.query(TABLE_NAME, columns, null, null, null, null, null);
}

public boolean easterEggWasFound() {
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_6, "1");
    guessTheNumber.update(TABLE_NAME, contentValues, null, null);
    return true;
}

public Cursor getIsEasterEggFound() {
    String[] easterColumns = {COL_6};
    return guessTheNumber.query(TABLE_NAME, easterColumns, null, null, null, null, null);
}

}

请告诉我为什么没有插入数据。

1 个答案:

答案 0 :(得分:-2)

同样的问题发生在我插入数据库时​​,经过大量的谷歌搜索和调试我找到了解决方案,

  

这里是解决方案请首都表和数据库的第一个字母   代码中的名称

public static final String DATABASE_NAME = "guessthenumber.db";
public static final String TABLE_NAME = "guess_the_number_table";

更改为

public static final String DATABASE_NAME = "Guessthenumber.db";
public static final String TABLE_NAME = "Uess_the_number_table";