mysql设值null 的正确姿势

mysql设值null 的正确姿势


问题:

偶然间遇到一个问题,mysql数据库中设置字段为null值后,判断为空时出现了错误。

代码如下:

 
System.out.println("account_type_db值="+account_type_db);//account_type_db值=null
boolean isNUll=(account_type_db == null) || (account_type_db.length () == 0);
System.out.println("isNUll="+isNUll);//isNUll=false
if (isNUll) {
    System.out.println("account_type_db值为空");
}else{
    System.out.println("account_type_db值不为空");
}
//account_type_db值不为空
------------System.out.println("account_type_db2值="+account_type_db2);//account_type_db2值=null
 
boolean isNUll2=(account_type_db2 == null) || (account_type_db2.length () == 0);
System.out.println("isNUll2="+isNUll2);//isNUll2=true
if (isNUll2) {
    System.out.println("account_type_db2值为空");
}else{
    System.out.println("aaccount_type_db2值不为空");
}
//account_type_db2值为空

解决:

查看数据库后发现

数据库的值如下:

正确的操作方式为:

update table_name  set account_type_db2=null where id=1;(这才是 设null值的正确sql)
update table_name  set account_type_db='null' where id=1;(此sql错误)
--错误的原因是因为mysql 将'null'识别为了字符串"null",而不是 NULL或""。

原文地址:

 https://blog.csdn.net/Annie_ya/article/details/79667463

如有侵权,请联系我删除,谢谢

猜你喜欢

转载自blog.csdn.net/GorgeousChou/article/details/86506071