0

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

3 回答 3

2

将您的表创建查询更改为

  database.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
                    KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    KEY_QUESTION_STATUS + " TEXT NOT NULL, " +
                    KEY_SCORE + " TEXT);");

因为您正在创建KEY_SCORE ,所以您必须为列TEXT NOT NULL传递值 KEY_SCORE

注意:更改表结构后,您需要更改数据库版本

于 2013-02-25T17:05:57.610 回答
1

setStatus(),调用sqlDatabase.insert()返回-1。这意味着您的数据无法插入。

尝试将其更改为sqlDatabase.insertOrThrow();这将在失败时抛出异常,这将更容易找出实际出了什么问题。

于 2013-02-25T17:04:48.187 回答
1

您还没有发布任何 LogCat 错误,因此可能还有其他问题。但是您必须为两者 设置一个值KEY_QUESTION_STATUSKEY_SCORE因为您指定它们不能null在表的定义中:

KEY_QUESTION_STATUS + " TEXT NOT NULL, " +
KEY_SCORE + " TEXT NOT NULL);");

当你尝试这个时会发生什么?

public long setStatus (String status) {         
    ContentValues dbContentValues = new ContentValues();
    dbContentValues.put(KEY_QUESTION_STATUS, status);
    dbContentValues.put(KEY_SCORE, "something");
    return sqlDatabase.insert(DATABASE_TABLE, null, dbContentValues);
}
于 2013-02-25T17:05:35.720 回答