我的代码有问题吗?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();
}