1

I am working with a simple database. It has three columns. _id (integer primary key autoincrement), jobnumber (text unique not null) and jobpart (text not null).

I am using a dialog with two EditText widgets to get the input and upon button press add it to the database.

When I enter a jobnumber but leave the jobpart empty I would expect that the record would not be added. However, it is being added and I get a blank jobpart. I was using "insert". Looking, I thought perhaps I needed to use "insertWithOnConflict" instead, but that didn't seem to make a difference, the same thing happened.

My next thought was perhaps an empty string did not count as a null value, so I tried the following:

if (jobpart == "") {
    jobpart1 = null;
}else{
    jobpart1 = jobpart;
}

Same thing happened, the database didn't even hiccup and put an empty slot in.

So I started looking through the SQL documentation. I'm wondering if I need to set the db into strict mode, as perhaps an implicit default is being used? Although the section I found that in related to servers, so I'm not sure it even applies here, and if it does, how and when I would do it. (At db creation? On db open?).

Any pointers appreciated.

1
  • Can you post the code you are using to do the insert? Commented Jan 22, 2012 at 19:20

2 Answers 2

3

When you compare strings you need to user the equals() method, not == as String is an object so == just compares pointers.

i.e. You want:

if (jobpart.equals("")) {
Sign up to request clarification or add additional context in comments.

1 Comment

Doh. I knew that from some long ago class. Made your recommended change and now everything works like it should. To summarize and relate it to my basic question/issue... an empty String is not equivalent to null, so expecting the SQL command to fail due to an empty line was not going to work. Thanks for the help!
2

try this

if (jobpart.matches("")) {
jobpart1 = null;
}else{
jobpart1 = jobpart;
}

or you can make sure they enter something in the edittexts

1 Comment

I'll probably end up making sure they enter something before I'm done, but for the moment it's bare bones only, so I'm going to start with the simple changing the empty string to a null. Thanks for the help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.