如何使数据库正确处理布尔值?

时间:2012-02-20 16:00:48

标签: android sql database sqlite

我创建了一个包含以下变量的数据库:

public static final String KEY_VALUE = "x";    public static final String KEY_ISROOT = "y";    public static final String KEY_ROWID = "_id";    public static final String KEY_REALROWID = "_ids";    public static final String KEY_ISPOWER = "z";    public static final String KEY_POWERORROOTNUMBER = "b";    public static final String KEY_ISDIVISOR = "a";    public static final String KEY_ISMULTIPLIER = "c";    public static final String KEY_ISADD = "d";    public static final String KEY_ISSUBTRACT = "e";    public static final String KEY_ISPS = "f";    public static final String KEY_ISPE = "g";    private static final String TAG = "NotesDbAdapter";    private DatabaseHelper mDbHelper;    private SQLiteDatabase mDb;    private static final String DATABASE_NAME = "data";    private static final String DATABASE_TABLE = "notes";    private static final int DATABASE_VERSION =1;    private static final String DATABASE_CREATE =           " create table " +DATABASE_TABLE+ " ("             +KEY_VALUE+ " double,  "            +KEY_REALROWID+ " integer,  "           +KEY_ROWID+ " integer primary key autoincrement,  "+KEY_ISPOWER+ " boolean,  "+KEY_ISDIVISOR+ " boolean,  "+KEY_ISMULTIPLIER+ " boolean,  "+KEY_ISADD+ " boolean,  "+KEY_ISSUBTRACT+ " boolean,  "+KEY_ISPS+ " boolean,  "+KEY_ISPE+ " boolean,  "+KEY_POWERORROOTNUMBER+ " double,  "          +KEY_ISROOT+ " boolean);"; 

插入行ID代码为:

public void createNote(double value, boolean isroot, boolean ispower, boolean ismultiply, boolean isdivisor, boolean add,           boolean issubtract, double roototpowerval, boolean paranthaseesend, boolean paranthaseesstart) {        ContentValues initialValues = new ContentValues();        initialValues.put(KEY_VALUE, value);        initialValues.put(KEY_ISROOT, isroot);        initialValues.put(KEY_ISPOWER, ispower);        initialValues.put(KEY_ISMULTIPLIER, ismultiply);        initialValues.put(KEY_ISDIVISOR, isdivisor);        initialValues.put(KEY_ISADD, add);        initialValues.put(KEY_ISSUBTRACT, issubtract);        initialValues.put(KEY_POWERORROOTNUMBER, roototpowerval);        initialValues.put(KEY_ISPE, paranthaseesend);        initialValues.put(KEY_ISPS, paranthaseesstart);      long r= mDb.insert(DATABASE_TABLE, null, initialValues);      int a = testCount();      ContentValues argsa = new ContentValues();      argsa.put(KEY_VALUE, value);      argsa.put(KEY_ISROOT, isroot);      argsa.put(KEY_ISPOWER, ispower);      argsa.put(KEY_ISMULTIPLIER, ismultiply);      argsa.put(KEY_ISDIVISOR, isdivisor);      argsa.put(KEY_ISADD, add);      argsa.put(KEY_ISSUBTRACT, issubtract);      argsa.put(KEY_POWERORROOTNUMBER, roototpowerval);      argsa.put(KEY_REALROWID, a);      argsa.put(KEY_ISPE, paranthaseesend);      argsa.put(KEY_ISPS, paranthaseesstart);      mDb.update(DATABASE_TABLE, argsa, KEY_ROWID + "=" + r, null);    }The fetchRow code is:    public Cursor fetchNote(long rowId) throws SQLException {        Cursor mCursor =            mDb.query(true, DATABASE_TABLE, new String[] {KEY_ISPS,KEY_ISPE,KEY_VALUE,KEY_ISROOT,KEY_REALROWID,KEY_ISPOWER,KEY_POWERORROOTNUMBER,KEY_ISDIVISOR,KEY_ISMULTIPLIER,KEY_ISADD,KEY_ISSUBTRACT}, KEY_REALROWID + "=" + rowId, null,                    null, null, null, null);        if (mCursor != null) {            mCursor.moveToFirst();        }        return mCursor;    }

它可以在这里正确地更改视图:

        public void onClick(View v) {               double d = 3;               mDbHelper.deleteAllNotes();                 mDbHelper.createNote(d, false, false, false, false, false, false, 0, false, false);                 mDbHelper.createNote(0, false, false, true,false, false, false, 0, false, false);               Cursor c = mDbHelper.fetchNote(1);if(d==Double.valueOf(c.getString(c.getColumnIndexOrThrow(NotesDbAdapter.KEY_VALUE)))){setContentView(R.layout.works);}            }

然而,它并没有改变观点:

public void onClick(View v) {               
    double d = 3;               
    mDbHelper.deleteAllNotes();                 
    mDbHelper.createNote(d, false, false, false, false, false, false, 0, false, false);             mDbHelper.createNote(0, false, false, true,false, false, false, 0, false, false);             Cursor c = mDbHelper.fetchNote(2);    
    if(Boolean.parseBoolean(c.getString(c.getColumnIndexOrThrow(NotesDbAdapter.KEY_ISMULTIPLIER)))){
    setContentView(R.layout.awesome);
}           
}

如何让SQL数据库正确发送/接收布尔值?

1 个答案:

答案 0 :(得分:2)

SQLite没有任何内置的布尔类型。你需要自己处理它。我通常使用和整数值,并将其设置为等于1或0表示true或false。然后,如果要检查数据库中是否为真,则检查值是否为1,等等。