简体   繁体   English

如何插入NULL long值?

[英]How do I insert a NULL long value?

I have a database made up a single table with several fields, one of which is a phone number that is stored as a NUMERIC. 我有一个数据库组成一个包含多个字段的表,其中一个是存储为NUMERIC的电话号码。 Because the phone number is 10 digits, in my code I have captured the variable as a LONG. 因为电话号码是10位数,在我的代码中我将变量捕获为LONG。

My question is, in the insert statement, how do I pass a NULL value? 我的问题是,在insert语句中,如何传递NULL值? Is this possible at all? 这有可能吗? I can't assign a null value to the LONG variable, so I'm wondering how to go about doing this... 我不能为LONG变量赋一个空值,所以我想知道如何去做这个......

Because the phone number is 10 digits, in my code I have captured the variable as a LONG. 因为电话号码是10位数,在我的代码中我将变量捕获为LONG。

Bad idea. 馊主意。 Phone numbers might look like numbers, but they're not. 电话号码可能看起来像数字,但它们不是。 They're strings of characters that happen to be mostly digits. 它们是恰好主要是数字的字符串。 But they can also contain symbols such as + . 但它们也可以包含+等符号。

Use a String instead. 请改用String


Update: If you are required to use a numeric type then make sure you use Long instead of long . 更新:如果您需要使用数字类型,请确保使用Long而不是long The capitilazation is important. 资本化非常重要。 And by the way, there is no such type as LONG in standard Java. 顺便说一句,标准Java中没有LONG这样的类型。

If you desperately need to use numerics for the phone number, why not just code a non-existing phone number into something like 0L or -1L? 如果您迫切需要使用数字作为电话号码,为什么不将不存在的电话号码编码为0L或-1L?

Edit: Btw, you say that the database is limited to numeric format. 编辑:顺便说一下,你说数据库仅限于数字格式。 Does the same go for the java side? java方面也一样吗?

long dbPhoneNumber = getTheLongFieldFromTheDatabase();
String internalPhoneNumberRepresentation = Long.toString(dbPhoneNumber);
doSomethingWithString();
dbPhoneNumber = internalPhoneNumberRepresentation == null ? 0L : internalPhoneNumberRepresentation;
storeLongFieldToDatabase(dbPhoneNumber);

Should more or less do all you need, right? 应该或多或少地做你需要的一切,对吧?

First of all, why do you store a phone number as a numeric field? 首先,为什么要将电话号码存储为数字字段? In most cases you probably also want to store things like "+41 (52) 123 455", which you loose with numeric datatypes, but ok. 在大多数情况下,你可能还想存储像“+41(52)123 455”这样的东西,你可以使用数值数据类型来保存,但是没问题。

Then for inserting null values: What about: 然后插入空值:怎么样:

INSERT INTO mytable (phone) VALUES (NULL);

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

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