问题
I have a number of EditTexts
on a UI where the user inputs data. The data is saved in a SQLite database
.
The user can leave some of the EditTexts
blank (no data entered). I confirmed in the database that the rows are blank for those EditTexts
. My question is: for the integrity of the database, should I be entering a blank String (the else
code shown below) for the empty EditTexts
? If so, how does entering the blank String help?
Possible code for the blank 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:
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.
来源:https://stackoverflow.com/questions/38152012/android-sqlite-will-a-blank-row-cause-any-database-issues