3

I have a SQLite Database and insert some data like this, but now I would like to check first if this value is already in the database and only if its not insert it. How I can do this?

myDB = this.openOrCreateDatabase(MY_DB_NAME, MODE_PRIVATE, null);
myDB.execSQL("INSERT INTO " + MY_DB_TABLE + "(db_datum)" 
 +"VALUES ('"+datum.getText().toString()+"');");
1
  • Please edit the your question, the inteded behaviour is not clear "... only if its now ...". Do you mean "only if it's not"? Commented Jul 31, 2011 at 16:39

2 Answers 2

1

Are you looking for an IF NOT EXISTS statement (that is, if a record doesn't exist then do something, in this case insert a record)? I'm a little confused myself. This may help: IF NOT EXISTS SQLite.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes I'm looking for an "IF NOT EXISTS" statement but now I have problems to implement this in android. Where I have to set the quotes?
0

you could do something like this:

 Cursor c = myDB.rawQuery("SELECT * FROM " + MY_DB_TABLE+ " WHERE " + FIELD + "= '" + VALUE + "'");
 if(c == null)
 {
 //doesn't exists therefore insert record.
 }

2 Comments

Does not handle concurrent operations, use this instead How to do IF NOT EXISTS in SQLite
I think you should use this if(c.getCount() == 0) { //doesn't exists therefore insert record. }

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.