我的数据库不会添加值。我有代码

时间:2013-02-25 17:00:54

标签: android database sqlite

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

3 个答案:

答案 0 :(得分: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列的值

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

答案 1 :(得分:1)

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

尝试将其更改为sqlDatabase.insertOrThrow();这将在失败时引发异常,这将使得更容易找出实际出现的问题。

答案 2 :(得分: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);
}