1

This is my Database Adapter class. I use it to connect and query the database.

package com.Coursework.AppointmentRecorder;

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

public class DBAdapter {
  public static final String KEY_ROWID = "_id";
  public static final String APP_TITLE = "title";
  public static final String APP_DATE = "date";
  public static final String APP_TIME = "time";
  public static final String APP_DETAILS = "details";
  private static final String TAG = "DBAdapter";
  private static final String DATABASE_NAME = "NewUser";
  private static final String DATABASE_TABLE = "Appointment";
  private static final int DATABASE_VERSION = 1;
  private static final String DATABASE_CREATE =
    "CREATE TABLE Appointment (_id integer primary key autoincrement, "
    + "title text not null, date text not null, time text not null, "
    + "details text not null);";
  private final Context context;

  private DatabaseHelper DBHelper;
  private SQLiteDatabase db;
  public DBAdapter(Context ctx) {
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
  }

  private static class DatabaseHelper extends SQLiteOpenHelper {
    DatabaseHelper(Context context) {
      super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

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

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

  public DBAdapter open() throws SQLException {
    db = DBHelper.getWritableDatabase();
    return this;
  }

  //---closes the database---
  public void close() {
    DBHelper.close();
  }

  //---insert a title into the database---
  public long insertAppointment(String title,String Date, String time, String details) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(APP_TITLE, title);
    initialValues.put(APP_DATE, Date);
    initialValues.put(APP_TIME, time);
    initialValues.put(APP_DETAILS, details);
    return db.insert(DATABASE_TABLE, null, initialValues);
  }

  //---deletes a particular title---
  public boolean deleteTitle(long rowId) {
    return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
  }

  //---retrieves all the titles---
  public Cursor getAllTitles() {
    return db.query(DATABASE_TABLE, new String[] {
      KEY_ROWID,
      APP_TITLE,
      APP_TIME,
      APP_DETAILS
    },
    null,
    null,
    null,
    null,
    null);
  }

  //---retrieves a particular title---
  public Cursor getTitle(long date) throws SQLException {
  Cursor mCursor = db.query(false, DATABASE_TABLE, new String[] {
    KEY_ROWID,
    APP_TITLE,
    APP_TIME,
    APP_DETAILS
  },
  APP_DATE + "=" + date,
  null,
  null,
  null,
  null,
  null);
  if (mCursor != null) {
    mCursor.moveToFirst();
    do {
    } while (mCursor.moveToNext());
  }
  return mCursor;
}

  //---updates a title---
  public boolean updateTitle(long rowId, String isbn, String title, String publisher) {
    ContentValues args = new ContentValues();
    args.put(APP_TITLE, isbn);
    args.put(APP_TIME, title);
    args.put(APP_DETAILS, publisher);
    return db.update(DATABASE_TABLE, args,
    KEY_ROWID + "=" + rowId, null) > 0;
  }
}

The issue I am having is that when I create an entry in another class, by opening the db as in first I call this:

final DBAdapter db = new DBAdapter(this);

And then I try to execute this command, but the catalog keeps saying there is no such table and giving me an SQL Exception.

db.open();
long id  = db.insertAppointment(Title.getText().toString(),
  String.valueOf(SDate),
  Jack.getText().toString(),
  details.getText().toString()
);
alertDialog.setTitle("Appoinment Manager");
alertDialog.setMessage("The Appointment Has Been Created.");
alertDialog.show();
db.close();

Can someone tell me where the mistake is and why the table is not being created?

1 Answer 1

3

I would guess you modified your database structure and forgot.to increment the version. This means you would still be using the old DB without the new table.

Increment your database version and run the app so the onUpgrade method runs and your problem should go.away.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.