0

I am having some problem while using sqlite database, and can't seem to find out the cause for the error. I have the following class

public class Greetings {
    //private variables
    int _id;
    String _phone_number;
    String _time;
    String _date;
    String _message;

   // Empty constructor
   public Greetings(){
   }

   // constructor
   public Greetings(int id, String date, String phone_number, String time, String message){
      this._id = id;
      this._date = date;
      this._phone_number = phone_number;
      this._time = time;
      this._message = message;
   }

   // constructor
   public Greetings(String date, String phone_number, String time, String message){
      this._date = date;
      this._phone_number = phone_number;
      this._time = time;
      this._message = message;
  }
  // getting ID
  public int getID(){
     return this._id;
  }

  // setting id
  public void setID(int id){
     this._id = id;
  }

  // getting name
  public String getDate(){
      return this._date;
  }

  // setting name
  public void setDate(String date){
      this._date = date;
  }

  // getting phone number
  public String getPhoneNumber(){
      return this._phone_number;
  }

  // setting phone number
  public void setPhoneNumber(String phone_number){
     this._phone_number = phone_number;
  }

  // getting phone number
  public String getTime(){
     return this._time;
  } 

  // setting phone number
  public void setTime(String time){
     this._time = time;
  }

  public String getMessage(){
      return this._message;
  }

  // setting phone number
  public void setMessage(String message){
     this._message = message;
  }
}

And the sqlitehelper class

public class SqliteController extends SQLiteOpenHelper{
    private static final String LOGCAT = null;
    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "sendGreetings";

    //  table name
    private static final String TABLE_GREETINGS = "greetings";

    // Greetings Table Columns names
    private static final String KEY_ID = "id";
    private static final String KEY_DATE = "date";
    private static final String KEY_PH_NO = "phone_number";
    private static final String KEY_TIME ="time";
    private static final String KEY_MESSAGE ="message";

    public SqliteController(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        Log.d(LOGCAT,"Created");
    }

   // Creating Tables
   @Override
   public void onCreate(SQLiteDatabase db) {
        String CREATE_GREETINGS_TABLE = "CREATE TABLE " + TABLE_GREETINGS + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_DATE + " TEXT,"
            + KEY_PH_NO + " TEXT," + KEY_TIME + "TEXT," + KEY_MESSAGE + "TEXT" + ")";
        db.execSQL(CREATE_GREETINGS_TABLE);
   }

   // Upgrading database
   @Override
   public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_GREETINGS);
       // Create tables again
       onCreate(db);
   }

   /**
     * All CRUD(Create, Read, Update, Delete) Operations
     */

   // Adding new greetings
   void addGreetings(Greetings greetings) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_DATE, greetings.getDate()); // Greetings Date
        values.put(KEY_PH_NO, greetings.getPhoneNumber()); // Greetings Phone
        values.put(KEY_TIME, greetings.getTime()); //Greetings time
        values.put(KEY_MESSAGE, greetings.getMessage());

        // Inserting Row
        db.insert(TABLE_GREETINGS, null, values);
        db.close(); // Closing database connection
    }

    // Getting single greeting
    Greetings getGreeting(int id) {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(TABLE_GREETINGS, new String[] { KEY_ID,
                    KEY_DATE, KEY_PH_NO, KEY_TIME, KEY_MESSAGE }, KEY_ID + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);
        if (cursor != null)
           cursor.moveToFirst();

        Greetings greeting = new Greetings(Integer.parseInt(cursor.getString(0)),
            cursor.getString(1), cursor.getString(2),cursor.getString(3),cursor.getString(4));
        // return contact
        return greeting;
    }

    // Getting All Greetings
    public List<Greetings> getAllGreetings() {
       List<Greetings> greetingList = new ArrayList<Greetings>();
       // Select All Query
       String selectQuery = "SELECT  * FROM " + TABLE_GREETINGS;

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

       // looping through all rows and adding to list
       if (cursor.moveToFirst()) {
         do {
            Greetings greeting = new Greetings();
            greeting.setID(Integer.parseInt(cursor.getString(0)));
            greeting.setDate(cursor.getString(1));
            greeting.setPhoneNumber(cursor.getString(2));
            greeting.setTime(cursor.getString(3));
            greeting.setMessage(cursor.getString(4));
            // Adding contact to list
            greetingList.add(greeting);
        } while (cursor.moveToNext());
      }

       // return greeting list
       return greetingList;
   }

   // Updating single greeting
   public int updateGreetings(Greetings greeting) {
      SQLiteDatabase db = this.getWritableDatabase();

      ContentValues values = new ContentValues();
      values.put(KEY_DATE, greeting.getDate());
      values.put(KEY_PH_NO, greeting.getPhoneNumber());
      values.put(KEY_TIME, greeting.getTime());
      values.put(KEY_MESSAGE, greeting.getMessage());

      // updating row
      return db.update(TABLE_GREETINGS, values, KEY_ID + " = ?",
            new String[] { String.valueOf(greeting.getID()) });
  }

  // Deleting single greeting
  public void deleteGreetings(Greetings greeting) {
     SQLiteDatabase db = this.getWritableDatabase();
     db.delete(TABLE_GREETINGS, KEY_ID + " = ?",
            new String[] { String.valueOf(greeting.getID()) });
     db.close();
  }

  // Getting greetings Count
  public int getGreetingsCount() {
     String countQuery = "SELECT  * FROM " + TABLE_GREETINGS;
     SQLiteDatabase db = this.getReadableDatabase();
     Cursor cursor = db.rawQuery(countQuery, null);
     cursor.close();

     // return count
     return cursor.getCount();
  }
}

Every time I execute the program by calling the following code,

SqliteController db = new SqliteController(this);
db.addGreetings(new Greetings(dt,txtPhoneNo,t,n));

I am getting the error "table greetings has no column named message" . Can someone help me figure out just where I am making a mistake. Thanks in advance

2
  • 1
    don´t know if this is relevant, I just see that in Your onCreate String, at "KEY_TIME" & "KEY_MESSAGE" is no blank before "TEXT,".... Commented Apr 21, 2015 at 14:00
  • you could check your sendGreetings.db file. Maybe you need update your DATABASE_VERSION number. Commented Apr 21, 2015 at 14:00

1 Answer 1

1

You need to add a space between KEY_MESSAGE and KEY_TIME and TEXT.So So rewrite CREATE_GREETINGS_TABLE statement as

 String CREATE_GREETINGS_TABLE = "CREATE TABLE " + TABLE_GREETINGS + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_DATE + " TEXT,"
            + KEY_PH_NO + " TEXT," + KEY_TIME + " TEXT," + KEY_MESSAGE + " TEXT" + ")";

And Don't forget to uninstall app before retry.

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

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.