编组和解编结果集:处理空字符串

发布于 2025-01-03 09:23:29 字数 128 浏览 3 评论 0原文

当我从 ResultSet 创建域对象的实例时,是否应该将 null 字符串转换为空字符串。相反,写入数据库时​​是否应该将空字符串转换为空字符串?

这里的最佳实践是什么?或者它仅仅取决于应用程序?

非常感谢。

When I'm creating instances of domain objects from a ResultSet, should I convert Strings which are null to an empty String. Conversely, when writing to the database should empty Strings be converted to null Strings?

What's best practise here? Or is it just dependent on the application?

Many thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

姜生凉生 2025-01-10 09:23:29

空字符串 "" 与其他字符串一样。对我来说,将 "" 存储为 NULL 与将 "" 存储为 '' 并存储“abc”NULL

所以,保持简单:"" 存储为 '',并将 null 引用存储为 空。


引用MySQL参考手册

对于 SQL 新手来说,NULL 值的概念是一个常见的混淆源,他们常常认为 NULL 与空字符串是一样的''

The empty string, "" is a string just as any other. To me it, storing "" as NULL makes as much sense as storing "" as '' and storing "abc" as NULL.

So, keep things simple: Store "" as '', and store a null reference as NULL.


To quote the MySQL reference manual:

The concept of the NULL value is a common source of confusion for newcomers to SQL, who often think that NULL is the same thing as an empty string ''.

哎呦我呸! 2025-01-10 09:23:29

IMO,你应该将空值保留为空值。根据您的数据库架构,空字符串和 null 是两个不同的值。如果您将这些值混为一谈,您的代码可能会产生意外的结果——尤其是当您有多个开发人员在同一代码库上工作时。您可能必须在下游代码中考虑这两种情况,但这很可能会更清晰且更可维护/可测试。

IMO, you should keep nulls as nulls. Depending on your DB schema, empty string and null are two distinct values. If you conflate those values, your code may produce unexpected results -- especially if you have multiple developers working on the same code base. You may have to account for both cases in downstream code, but that will most likely be clearer and more maintainable/testable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文