Home > Enterprise >  Force Close when Adding and Showing data from internal database SQLite Android
Force Close when Adding and Showing data from internal database SQLite Android

Time:04-13

This is my MainActivity Class

public class MainActivity extends AppCompatActivity {

    TextView textView;
    String text;

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

        textView = (TextView) findViewById(R.id.textView);
        DatabaseHelper db = new DatabaseHelper(this);

        //inserting
        db.addMessage(new Message("Halo pesan 1", "group1"));
        db.addMessage(new Message("Halo pesan 2", "group1"));
        db.addMessage(new Message("Halo pesan 3", "group2"));
        db.addMessage(new Message("Halo pesan 4", "group2"));

        //reading and displaying all message
        List<Message> messageList = db.getAllMessage();

        for(Message m : messageList){
            String log = "ID "   m.getId()   ", Message: "   m.getMessageText()   ", Sender: "   m.getSender()   "\n";
            text = text   log;
        }

        textView.setText(messageList.toString());

    }
}

This is my Database Class

public class DatabaseHelper extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "messageManager";
    private static final String TABLE_MESSAGE = "m_message";
    private static final String KEY_ID = "id";
    private static final String KEY_MESSAGE = "messageText";
    private static final String KEY_SENDER = "sender";

    public DatabaseHelper(Context context){
        super(context, DATABASE_NAME,null,DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db){
        String CREATE_MESSAGE_TABLE = " CREATE TABLE "   TABLE_MESSAGE   "("
                 KEY_ID   " INTEGER PRIMARY KEY , "   KEY_MESSAGE   "TEXT , "
                 KEY_SENDER   "TEXT"   " )";
        db.execSQL(CREATE_MESSAGE_TABLE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int i , int b){
        db.execSQL("DROP TABLE IF EXISTS "   TABLE_MESSAGE);

        onCreate(db);
    }

    void addMessage(Message message){
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_MESSAGE, message.getMessageText());
        values.put(KEY_SENDER, message.getSender());

        db.insert(TABLE_MESSAGE,null,values);
        db.close();

    }

    public List<Message> getAllMessage(){
        List<Message> messageList = new ArrayList<>();

        String selectQuery = " Select * FROM "   TABLE_MESSAGE;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery,null);

        if (cursor.moveToFirst()){
            do{
                Message message = new Message();
                message.setId(Integer.parseInt(cursor.getString(0)));
                message.setMessageText(cursor.getString(1));
                message.setSender(cursor.getString(2));

                messageList.add(message);
            }while (cursor.moveToNext());
        }
        return messageList;
    }

}

and this is my model class

public class Message {

    int id;
    String messageText;
    String sender;

    public Message(){

    }

    public Message(String messageText , String sender){
        this.messageText = messageText;
        this.sender = sender;

    }

    public Message(int id , String messageText , String sender){
        this.id = id;
        this.messageText = messageText;
        this.sender = sender;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getMessageText() {
        return messageText;
    }

    public void setMessageText(String messageText) {
        this.messageText = messageText;
    }

    public String getSender() {
        return sender;
    }

    public void setSender(String sender) {
        this.sender = sender;
    }


}

This is the debug log

I simply just want to add some data to database and showing it to screen. There is no error when running it, but when I debuging it there is something wrong. I've changed the database name nor table name but still force close. In my analysis there is something wrong with creating a database but I don't know how to solve it.

CodePudding user response:

replace oncreate CREATE_MESSAGE_TABLE string

String CREATE_MESSAGE_TABLE = "CREATE TABLE "   TABLE_MESSAGE   "("
                 KEY_ID   " INTEGER PRIMARY KEY,"   KEY_MESSAGE   " TEXT,"
                 KEY_SENDER   " TEXT"   ")";
  • Related