0

I have 4 button on MainActvity. When I click on Add Button , then AddData class is opened. Here I have to insert Name,Phone Number and Email. When I click into
save button of AddData class then Toast is shown with message "Data is not inserted."

Sorry for my grammar.

MainActivity.java

package csitmnr.sqlitedatabaseexample;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
  Button search,add,delete,update;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    search = (Button) findViewById(R.id.btnSearch);
    add = (Button) findViewById(R.id.btnAdd);
    delete = (Button) findViewById(R.id.btnDelete);
    update = (Button) findViewById(R.id.btnUpdate);

    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(MainActivity.this,AddData.class);
            startActivity(intent);
        }
    });
}
}

DBHelper.java

package csitmnr.sqlitedatabaseexample;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;



public class DBHelper extends SQLiteOpenHelper {

   private static final String DATABASE_NAME = "contacts.db";
   private static final String TABLE_NAME = "contacts";

   public DBHelper(Context context) {
     super(context, DATABASE_NAME, null, 1);


   }

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    sqLiteDatabase.execSQL("CREATE TABLE "+TABLE_NAME+"(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,PHONE_NO TEXT,EMAIL TEXT)");


}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    sqLiteDatabase.execSQL("DROP TABLE IF EXIST "+TABLE_NAME);
    onCreate(sqLiteDatabase);
}

public boolean insertData(String name,String phone_no,String email){

    SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();

    ContentValues contentValues = new ContentValues();

    contentValues.put("NAME",name);
    contentValues.put("PHONE_NO",phone_no);
    contentValues.put("EMAIL",email);

    long result = sqLiteDatabase.insert(TABLE_NAME,null,contentValues);

    if (result == -1)
        return false;
    else
        return true;


}
}

AddData.java

package csitmnr.sqlitedatabaseexample;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AddData extends AppCompatActivity {
  EditText name , phone, email;
  Button save;
  private DBHelper dbHelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_data);

    dbHelper = new DBHelper(this);

    name = (EditText) findViewById(R.id.etName);
    phone = (EditText) findViewById(R.id.etPhone);
    email = (EditText) findViewById(R.id.etEmail);
    save = (Button) findViewById(R.id.saveButton);


    save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            boolean result = dbHelper.insertData(name.getText().toString(),phone.getText().toString(),email.getText().toString());
                if (result){
                    Toast.makeText(AddData.this, "Data is inserted", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(AddData.this, "Data is not inserted", Toast.LENGTH_SHORT).show();
                }
        }
    });
}
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Manoj Budha Ayer
  • 325
  • 2
  • 14

1 Answers1

0

From the comments:

Since your app is already created a table, try to clear the data first and run your app again. – Jerrol 1 min ago

Easy solution, since you are disposing the old table on upgrade, just try increasing your table version.

The cause of the problem would be that you changed the format of your database without increasing the version number, so possible solutions are:

  • Increase the version number and implement onUpgrade() properly
  • Reinstall the app (assuming non-persistent storage)
  • Clear the app data
Mars
  • 2,505
  • 17
  • 26