0

I am new in Android development, And right now what I am trying to do is to save Message that I type and Username. I got this code from vogella.com, but it was written only for messages. And I have tried to add Username, but I get an error "java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testdatabaseactivity/com.example.testdatabaseactivity.TestDatabaseActivity}: android.database.sqlite.SQLiteException: no such column: SENDER (code 1): , while compiling: SELECT _id, SENDER, MESSAGE FROM MESSAGES" And I can't figure out what Is wrong. Need a little help.

MessagesDataSource.java

package com.example.testdatabaseactivity;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class MessagesDataSource {

  // Database fields
  private SQLiteDatabase database;
  private MySQLiteHelper dbHelper;
  private String[] allColumns = { MySQLiteHelper.COLUMN_ID, MySQLiteHelper.COLUMN_SENDER, MySQLiteHelper.COLUMN_MESSAGE};

  public MessagesDataSource(Context context) {
    dbHelper = new MySQLiteHelper(context);
  }

  public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
  }

  public void close() {
    dbHelper.close();
  }

  public Message createMessage(String sender, String message) {
    ContentValues values = new ContentValues();
    values.put(MySQLiteHelper.COLUMN_MESSAGE, message);
    values.put(MySQLiteHelper.COLUMN_SENDER, sender);
    long insertId = database.insert(MySQLiteHelper.TABLE_MESSAGES, null,values);
    Cursor cursor = database.query(MySQLiteHelper.TABLE_MESSAGES,allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,null, null, null);
    cursor.moveToFirst();
    Message newMessage = cursorToMessage(cursor);
    cursor.close();
    return newMessage;
  }

  public List<Message> getAllMessages() {
    List<Message> messages = new ArrayList<Message>();

    Cursor cursor = database.query(MySQLiteHelper.TABLE_MESSAGES,allColumns, null, null, null, null, null);

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
      Message message = cursorToMessage(cursor);
      messages.add(message);
      cursor.moveToNext();
    }
    // make sure to close the cursor
    cursor.close();
    return messages;
  }

  private Message cursorToMessage(Cursor cursor) {
    Message message = new Message();
    message.setId(cursor.getLong(0));
    message.setMessage(cursor.getString(2));
    message.setSender(cursor.getString(1));
    return message;
  }
}

Message.java

      package com.example.testdatabaseactivity;

      public class Message {
  private long id;
  private String message;
  private String sender;

  public long getId() {
    return id;
  }

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

  public String getMessage() {
    return message;
  }

  public void setMessage(String message) {
    this.message = message;
  }
  public String getSender() {
        return sender;
      }

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

  // Will be used by the ArrayAdapter in the ListView
  @Override
  public String toString() {
    return message;
  }
} 

MySQLiteHelper.java

package com.example.testdatabaseactivity;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class MySQLiteHelper extends SQLiteOpenHelper {

  public static final String TABLE_MESSAGES = "MESSAGES";
  public static final String COLUMN_ID = "_id";
  public static final String COLUMN_MESSAGE = "MESSAGE";
  public static final String COLUMN_SENDER = "SENDER";

  private static final String DATABASE_NAME = "message.db";
  private static final int DATABASE_VERSION = 1;

  // Database creation sql statement
  private static final String DATABASE_CREATE = "create table " + TABLE_MESSAGES + "(" 
  + COLUMN_ID + " integer primary key autoincrement, " 
  + COLUMN_SENDER + "sender not null, " 
  + COLUMN_MESSAGE + " text not null);";

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

  @Override
  public void onCreate(SQLiteDatabase database) {
    database.execSQL(DATABASE_CREATE);

  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w(MySQLiteHelper.class.getName(),"Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_MESSAGES);
    onCreate(db);
  }

} 

TestDatabaseActivity.java

package com.example.testdatabaseactivity;

import java.util.List;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;

public class TestDatabaseActivity extends ListActivity {
  private MessagesDataSource datasource;
  public String sender = "Suren";
  EditText edittext;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    edittext = (EditText) findViewById(R.id.txt_message);
    datasource = new MessagesDataSource(this);
    datasource.open();
    List<Message> values = datasource.getAllMessages();

    // use the SimpleCursorAdapter to show the
    // elements in a ListView
    ArrayAdapter<Message> adapter = new ArrayAdapter<Message>(this,android.R.layout.simple_list_item_1, values);
    setListAdapter(adapter);
  }

  // Will be called via the onClick attribute
  // of the buttons in main.xml
  public void onClick(View view) {
    @SuppressWarnings("unchecked")
    String text = edittext.getText().toString();
    ArrayAdapter<Message> adapter = (ArrayAdapter<Message>) getListAdapter();
    Message message = null;
      if(edittext.length() == 0){
        return;
     }else{
      // save the new message to the database
      message = datasource.createMessage(sender, text);
      adapter.add(message);
      edittext.getText().clear();

   }
    adapter.notifyDataSetChanged();
  }

  @Override
  protected void onResume() {
    datasource.open();
    super.onResume();
  }

  @Override
  protected void onPause() {
    datasource.close();
    super.onPause();
  }

} 
1

2 Answers 2

2

Correct your CREATE Table Query need space between Column Name and Column Type

// Database creation sql statement
private static final String DATABASE_CREATE = "create table " + TABLE_MESSAGES + "(" 
+ COLUMN_ID + " integer primary key autoincrement, " 
+ COLUMN_SENDER + " text not null, " 
+ COLUMN_MESSAGE + " text not null);";

You Forget to add space over here in your Query COLUMN_SENDER + "sender not null, "

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

1 Comment

Oh God Thanks a lot. I was looking on that peace of code for probably 3 hours and didn't see that. thanks again :)
2

// Database creation sql statement

private static final String DATABASE_CREATE = "CREATE TABLE " + TABLE_MESSAGES + "(" 
  + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " 
  + COLUMN_SENDER + " text not null, " 
  + COLUMN_MESSAGE + " text not null)";

"sender" is not a type. You need put "text". And give the appropriate spaces.

1 Comment

@MM also +1 up for your answer

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.