6

I am developing an application which requires use of an SQLite database. I have implemented fetching the data, but I am facing problems when I try to insert data. The problem that I am having is that the new data I enter is not stored, i.e nothing is new is being entered into the database.

This is my insert code, myDataBase is an instance of SQLiteDatabase.

public void insertTitle(String Recipe)  
{  
    ContentValues initialValues = new ContentValues();  
    initialValues.put(COLUMN_NAME,value);  
    myDataBase.insert(ZRECIPE, null, initialValues);  
 }
1
  • What is the return value from insert(): -1 (error), or a valid row ID? Commented Nov 13, 2017 at 18:41

3 Answers 3

5

Try it this way. You need first start transaction, then mark it as successful and end.

   try {  
       myDataBase.beginTransaction();
       myDataBase.insert(ZRECIPE, null, initialValues);
       myDataBase.setTransactionSuccessful();
       } 
   finally {
       myDataBase.endTransaction();
       }
Sign up to request clarification or add additional context in comments.

1 Comment

Is it necessary to do an explicit transaction in order to get it to commit? Doesn't SQLite use autocommit mode by default?
2

You forgot to commit the transaction.

2 Comments

Hi, but how to commit the transcaction..?
Not sure but I guess that setTransactionSuccessful() actually is the commit.
0

Try with this code this may help you

public void insertTitle(String Recipe)  
{  
    ContentValues initialValues = new ContentValues();  
    initialValues.put(COLUMN_NAME,Recipe);  
    myDataBase.insert(ZRECIPE, null, initialValues);  
 }

Comments

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.