English 中文(简体)
为什么我在爪哇/安道尔德的空SQLite场 得到"0.0"的"0.0"?
原标题:Why am I getting a "0.0" for an empty SQLite field in Java/Android?

代码:

double indSRate[] = new double[c.getCount()];
indSRate[e++] = c.getDouble(c.getColumnIndex("s_rate"));


if (String.valueOf(indSRate[s]) == null) {
    Log.d("Special Rate", String.valueOf(indSRate[s++]));
}

S_read 列中的字段被编辑文本字段中的用户留空。 我是否将它转换成双倍?

在循环中, 如果字段是空的, 我想做某些事情, 如果字段是空的, 如果不是空的, 我想做其他事情。 但是它看起来是0 或更大? 我不能测试它是0 或不同的东西。 我必须在空的之间做测试吗?

最佳回答

如果一个数值的内部表示为 NULLL ,而您的查询要求为 FLOAT ,则您可以得到 0.0 的返回值(见 这份文件 )。

如果您不希望光标中该列的 NULL 值, 请在查询中指定该值。 如果您在数据库中不想要 NULL 值, 请在创建表格时用列限制来指定该值。

问题回答

原始数据类型, 如双元和元不能是空的或空的 。 当您将数据转换为字符串时, 默认值分别为“ 0.0 ” 和“ 0 ” 。

<强 > 添加

您可以检查双 s 值是否为 0.0 :

if(indSRate[s] == 0.0) { // default value
    Log.d("Special Rate", String.valueOf(indSRate[s++]));
}

或:

int rateIndex = c.getColumnIndex("s_rate");
while(c.moveToNext()) {
    if(c.isNull(rateIndex))
        Log.d("Special Rate", "Null");
    indSRate[e++] = c.getDouble(rateIndex);
}

有很多解决方案...

如果列的值为 NULL, 则结果Set. getDouble 返回 0.0。 如果您想要在使用原始双倍时区分 0.0 和 NULL, 您必须明确检查结果Set. was Null () 。 或者, 您可以获得结果Set. getObject () 并投入双倍。 在这种情况下, 您将获得无效引用, 您显然可以对无效进行检查 。

双倍表示为 0.0, 因此空时显示为 0.0。 请试此 :

String s = null;

double indSRate[] = new double[c.getCount()];

indSRate[e++] = c.getDouble(c.getColumnIndex("s_rate"));

if (String.valueOf(indSRate[s]) == null) {
    Log.d("Special Rate", s));
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签