I am a student and started working on android studio recently. I don’t know about it much. I am working on an application where I save the item name and its amount in the database and display toast message if data we entered is saved or not. problem is whenever I click on the save button my application crashes.
following is my DatabaseHelper class:
public class DatabaseHelper extends SQLiteOpenHelper { public static final String DATABASE_NAME = "Items.db"; public static final String TABLE_NAME = "item_table"; public static final String COL_1 = "ID"; public static final String COL_2 = "ITEM"; public static final String COL_3 = "AMOUNT"; public DatabaseHelper(@Nullable Context context) { super(context, DATABASE_NAME, null, 1); } @Override public void onCreate(SQLiteDatabase db) { String createTable = "CREATE TABLE" + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT," + "ITEM TEXT, AMOUNT TEXT)"; db.execSQL(createTable); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP IF TABLE EXISTS" + TABLE_NAME); onCreate(db); } public boolean addData(String item, String amount){ SQLiteDatabase db = this.getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put(COL_2,item); contentValues.put(COL_3,item); long result = db.insert(TABLE_NAME,null, contentValues); if(result == -1){ return false; }else { return true; } } }
following is my MainActivity class:
public class MainActivity extends AppCompatActivity { DatabaseHelper myDb; EditText editItem, editAmount; Button buttonSave; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myDb = new DatabaseHelper(this); editItem = (EditText)findViewById(R.id.item_field); editAmount = (EditText)findViewById(R.id.amount_field); buttonSave = (Button)findViewById(R.id.button_save); AddData(); } public void AddData(){ buttonSave.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String item = editItem.getText().toString(); String amount = editAmount.getText().toString(); boolean insertData = myDb.addData(item, amount); if(insertData == true){ Toast.makeText(MainActivity.this,"Amount is saved with Item detail", Toast.LENGTH_LONG).show(); }else { Toast.makeText(MainActivity.this, "Error occurred : Detailed are not saved", Toast.LENGTH_LONG).show(); } } }); } }
I will appreciate your help. Thank you
Advertisement
Answer
It might be crashing because it’s not creating the table:
String createTable = "CREATE TABLE" + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT," + "ITEM TEXT, AMOUNT TEXT)";
A space is missing between table and its name:
String createTable = "CREATE TABLE " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, ITEM TEXT, AMOUNT TEXT)";
You should also close the database after you get the data in db.insert but this only creates a warning:
long result = db.insert(TABLE_NAME,null, contentValues); db.close();