gpt4 book ai didi

java - 无法正确存储 sqlite

转载 作者:行者123 更新时间:2023-12-01 13:37:11 26 4
gpt4 key购买 nike

我想设置if语句来控制sqlite中的数据存储。但是,数据将被全部存储或全部不存储,因此if语句不能阻止不正确的数据。请给我一些提示。谢谢。

public void luggageCheck (String ncode, String luggage1, String luggage2, String luggage3){

DatabaseHandler db = new DatabaseHandler(this);

if (luggage1 != " " ){
db.addContact(new Luggage(ncode, luggage1, "luggage"));
}
if (luggage2 != " "){
db.addContact(new Luggage(ncode, luggage2, "luggage"));
}
if (luggage3 != " "){
db.addContact(new Luggage(ncode, luggage3, "luggage"));
}

}

最佳答案

Java中的字符串是通过“equals”方法进行比较的,而不是通过运算符进行比较的

这意味着您的代码应该是:

if (!luggage1.equals(" ")){
db.addContact(new Luggage(ncode, luggage1, "luggage"));
}
if (!luggage2.equals(" ")){
db.addContact(new Luggage(ncode, luggage2, "luggage"));
}
if (!luggage3.equals(" ")){
db.addContact(new Luggage(ncode, luggage3, "luggage"));
}

关于java - 无法正确存储 sqlite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21180465/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com