我的代碼有什麼問題嗎? setStatus總是返回-1。一直在追蹤錯誤的代碼,但我找不到它。也許你們在這裏可以。非常令人沮喪。我的數據庫不會添加值。我的代碼是
這是我怎麼叫數據庫:
SQLDatabaseLevelOne sql;
sql = new SQLDatabaseLevelOne(MainActivity.this);
sql.open();
sql.setStatus("1");
sql.close();
這是從我SQLDatabaseLevelOne.class:
public static final String KEY_ID = "_id";
public static final String KEY_QUESTION_STATUS = "_STATUS";
public static final String KEY_SCORE = "_SCORE";
//public static final String KEY_INDEX = "_INDEX";
private static final String DATABASE_NAME = "passworddb";
private static final String DATABASE_TABLE = "passwordtable";
private static final int DATABASE_VERSION = 1;
//private int index_score;
private DbHelper dbHelper;
private final Context dbContext;
private SQLiteDatabase sqlDatabase;
private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
/*database.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_QUESTION_STATUS + " TEXT NOT NULL, " +
KEY_SCORE + " TEXT NOT NULL, " +
KEY_INDEX + " TEXT NOT NULL);");*/
database.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_QUESTION_STATUS + " TEXT NOT NULL, " +
KEY_SCORE + " TEXT NOT NULL);");
}
@Override
public void onUpgrade(SQLiteDatabase database, int arg1, int arg2) {
database.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(database);
}
}
public SQLDatabaseLevelOne(Context context) {
dbContext = context;
}
public long setStatus (String status) {
ContentValues dbContentValues = new ContentValues();
dbContentValues.put(KEY_QUESTION_STATUS, status);
//dbContentValues.put(KEY_SCORE, score);
return sqlDatabase.insert(DATABASE_TABLE, null, dbContentValues);
}
public void open() {
dbHelper = new DbHelper(dbContext);
sqlDatabase = dbHelper.getWritableDatabase();
return;
}
public void close() {
dbHelper.close();
}
因爲「KEY_SCORE TEXT NOT NULL」有一個約束(NOT NULL),因爲你不給它一個值。 – njzk2 2013-02-25 17:05:19
我不知道該如何謝謝大家。所有的答覆都是正確的。我接受了答案。我一直在使用NOT NULL而不考慮對數據庫的影響。 – droidH 2013-02-25 17:18:14
沒關係,我們都有足夠的分數。在未來的某一天,通過幫助別人簡單地「付出代價」。 – Sam 2013-02-25 17:20:33