Android& SQLite:一行是否会导致任何数据库问题?

时间:2016-07-01 18:37:47

标签: android string sqlite

我在用户输入数据的UI上有一些EditTexts。数据保存在SQLite database

用户可以留下一些EditTexts空白(未输入数据)。我在数据库中确认那些EditTexts的行是空白的。我的问题是:为了数据库的完整性,我应该为空else输入一个空字符串(下面显示的EditTexts代码)吗?如果是这样,输入空白字符串如何帮助?

空白EditTexts的可能代码:

...
et_name = (EditText) findViewById(R.id.et_name);

@Override
public void onClick(View v) {
    UserData userData = new UserData();
    if(!et_name.getText().toString().isEmpty() {
        userData.name = et_name.getText().toString();
    } else {
        userData.name = "";
    }
    ...

1 个答案:

答案 0 :(得分:1)

For the integrity of the database, it is conceptually better to not set a value for the column or otherwise set it to null. Null indicates no value is set and in this case the user did indeed provide no value. Using null gives you a clear indication that no value has been provided and is easier to check for in SQL queries. Storing an empty string also uses storage space that null does not.

This is however not a hard and fast rule. If storing no value as an empty string has benefits to your code and logic then feel free to do so.