简体   繁体   English

为什么从SQLite分配Android游标会为true返回0

[英]Why is the assignment of Android cursor from SQLite returning 0 for true

I'm developing an Android app, whilst learning, that accesses an SQLite database which holds some boolean data (amongst other data) in columns but I'm confused about how the boolean data is being represented. 在学习的同时,我正在开发一个Android应用程序,该应用程序访问一个SQLite数据库,该数据库在列中保存了一些布尔数据(以及其他数据),但是我对布尔数据的表示方式感到困惑。

As I understood it, for SQLite purposes, 0 is false and 1 is true. 据我了解,出于SQLite的目的,0为false,1为true。 This is bourne out by my searches regarding my problem (see get-boolean-from-database-using-android-and-sqlite and SQLite Docs for two examples). 对于我的问题,这是我搜索的结果(有关两个示例,请参见从数据库使用AndroidSQLite 获取布尔值SQLite文档 )。

Regarding my particular problem, the table I am querying has two boolean columns and in the row in question, both columns are 'true' at this moment in time. 关于我的特定问题,我正在查询的表具有两个布尔列,并且在所讨论的行中,这两个列在此刻均为“ true”。 However, when I assign the values from these columns in my app, they are 'false'. 但是,当我从应用程序中的这些列中分配值时,它们为“ false”。 Here is the scenario in code terms; 这是代码形式的场景;

SQLiteDatabase db = getReadableDatabase();
String[] result_cols = new String[] { KEY, FD_CONTACT_FNAME, FD_CONTACT_SNAME, FD_IS_PUPIL, FD_IS_ACTIVE };
String where = KEY + " = ?";
String whereArgs[] = {id};
String groupBy = null;
String having = null;
String order = null;

Cursor cursor = db.query(DB_TABLE_CONTACTS, result_cols, where, whereArgs, groupBy, having, order);
Log.d("ACONTACT", DatabaseUtils.dumpCursorToString(cursor));

while (cursor.moveToNext()) {
    int three = cursor.getInt(3);
    int four = cursor.getInt(4);

    if (cursor.getInt(3) == 1) {
        Log.d("ONE", "1");
    } else if (cursor.getInt(3) == 0) {
        Log.d("ZERO", "0");
    }

    Contact mContact = new Contact(cursor.getLong(0), cursor.getString(1), cursor.getString(2), ((cursor.getInt(3) == 1)? true : false), (cursor.getInt(4) > 0));
    Log.d("ACONTACT", String.valueOf(mContact.getIsPupil()) + " .... " + String.valueOf(mContact.getIsActive()));
    Log.d("ACONTACT", String.valueOf(cursor.getInt(3)) + " .... " + String.valueOf(cursor.getInt(4)));
    Log.d("ACONTACT", cursor.getString(1) + " .... " + cursor.getString(2));
    Log.d("ACONTACT", String.valueOf(cursor.getLong(0)));
    return  mContact;
}

The logging aspect of the code results in; 代码的日志记录方面导致;

D/ACONTACT: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@418c5f28
        0 {
           _id=24
           fname=Harry
           sname=Ballard
           is_pupil=true
           is_active=true
        }
        <<<<<
D/ZERO: 0
D/ACONTACT: false .... false
D/ACONTACT: 0 .... 0
D/ACONTACT: Harry .... Ballard
D/ACONTACT: 24

'Contact' is a custom class, nothing complicated, that has a constructer like so; “联系人”是一个自定义类,没有什么复杂的,它具有类似的构造函数;

    public Contact( long id, String fname, String sname, Boolean isPupil, Boolean isActive ) {
    this.id = id;
    this.fname = fname ;
    this.sname = sname ;
    this.isPupil = isPupil;
    this.isActive = isActive;
}

I can see from the logging that the cursor is correct when it is dumped by 'DatabaseUtils' but when I read the cursor values, either into my class or into any other variables for debugging, they appear wrong (both int 'three' and int 'four' hold zero for example, so false, after assignment). 从日志记录中我可以看到,当游标被“ DatabaseUtils”转储时,游标是正确的,但是当我将游标值读入我的类或任何其他用于调试的变量时,它们显示为错误(int'three'和int例如,“四”保持零,因此赋值后为假)。 The data in the three other columns is always correct. 其他三列中的数据始终正确。

I'm obviously missing something but as I see it, the cursor holds the correct data (two boolean columns, both with values of 'true', as seen by cursor dump) but then cursor.getInt(3) and cursor.getInt(4) both return zero, ie false. 我显然丢失了一些东西,但是正如我看到的那样,游标保存了正确的数据(两个布尔列,都具有'true'值,如游标转储所示),然后是cursor.getInt(3)和cursor.getInt( 4)两者都返回零,即false。 How can the cursor appear to change, it's doing my head in :) 光标看起来如何改变,它正在:)

Have said all that, my title probably isn't accurate, as the cursor is at first correct but then something goes wrong. 说了这么多,我的标题可能不正确,因为光标最初是正确的,但随后出了点问题。 Anybody any clues as to what? 有人对什么有任何线索吗?

When the documentation says that "Boolean values are stored as integers 0 (false) and 1 (true)", then it means exactly that: you have to store them as the numbers 0 and 1 . 文档中说“布尔值存储为整数0(false)和1(true)”时,这恰恰意味着:您必须将它们存储为数字01

At the moment, you are storing the strings true or false in the database. 目前,您正在将字符串 truefalse存储在数据库中。 These values cannot be converted into an integer. 这些值不能转换为整数。

Ok, my apologies for the noise. 好的,我很抱歉听到这个声音。 The boolean data was inserted into the database using literal values 'true' and 'false'. 使用文字值“ true”和“ false”将布尔数据插入数据库中。 Therefore, using integer logic ('cursor.getInt(0) > 0' or equivalent) to determine true or false always returned false because the value held was literally 'true' and not '1'. 因此,使用整数逻辑('cursor.getInt(0)> 0'或等效值)来确定true或false总是返回false,因为所保存的值实际上是'true'而不是'1'。

I hope that's clear. 我希望这很清楚。 Again, apologies for the noise. 再次,对噪声表示歉意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM