0

So every time I enter data into my EditText field, which is located in my AddContacts class, my Dialog says that I have successfully added the information, but in the log cat it says no such table: contactsTable. I think the error is within the onCreate and onUpdate methods in my database class, but I am not sure what to do

Code:

package com.example.myfirstapp;

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;


public class DatabaseTable {
private static final String TAG = "ContactsDatabase";

//The columns we'll include in the contacts table
public static final String COL_ID = "ID";
public static final String COL_NAME = "NAME";
public static final String COL_EMAIL = "EMAIL";

private static final String DATABASE_NAME = "CONTACT";
private static final String DATABASE_TABLE = "contactsTable";
private static final int DATABASE_VERSION = 1;
private Context ourContext;
private DbHelper DBHelper;
private SQLiteDatabase db;
private static final String DATABASE_CREATE = 
        "CREATE TABLE " + DATABASE_TABLE + " (" +
         COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
         COL_NAME + " TEXT NOT NULL, " + 
         COL_EMAIL + " TEXT NOT NULL);";

    private static class DbHelper extends SQLiteOpenHelper {

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

        public void onCreate(SQLiteDatabase db) {
            try {
                db.execSQL(DATABASE_CREATE);
            } catch (SQLException e) {
                e.printStackTrace();
            }
    }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
            onCreate(db);
        }
    }

        public DatabaseTable(Context context) {
            ourContext = context;
        }

        public DatabaseTable open() throws SQLException {
            DBHelper = new DbHelper(ourContext);
            db = DBHelper.getWritableDatabase();
            return this;
        }

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

        public long insertContact(String name, String email) {
            ContentValues initialValues = new ContentValues();
            initialValues.put(COL_NAME, name);
            initialValues.put(COL_EMAIL, email);
            return db.insert(DATABASE_TABLE, null, initialValues);
        }

        public void deleteRecord(long rowId)
        {
            db.delete(DATABASE_TABLE, COL_ID + "=" + rowId, null);
        }

        public Cursor getAllRecords() {
            return db.query(DATABASE_TABLE, new String[] {COL_ID, COL_NAME, COL_EMAIL}, null, 
                    null, null, null, null);
        }

        public Cursor getRecord(long rowId) throws SQLException {

            Cursor cursor = db.query(true, DATABASE_TABLE, new String[] {COL_ID, COL_NAME,
                    COL_EMAIL}, COL_ID + "=" + rowId, null, null, null, null, null);

            if (cursor != null)
                cursor.moveToFirst();

            return cursor;
            }

        public boolean updateRecord(long rowId, String name, String email) {
            ContentValues content = new ContentValues();
            content.put(COL_NAME, name);
            content.put(COL_EMAIL, email);
            return db.update(DATABASE_TABLE, content, COL_ID + "=" + rowId, null) > 0;
        }      

}

2
  • Did you change DATABASE_TABLE after running your app at least once? If so you need to update your SQLite schema, the easiest way to do this is make DATABASE_VERSION = 2;. Also Android is very particular about the primary key, you should change COL_ID to "_id". Commented Dec 26, 2012 at 18:26
  • Yes I did change it. I switched my version number to 2 and COL _KEY to "_id". I think the problem was the version number because now it is working, so thank you very much. Commented Dec 26, 2012 at 18:33

1 Answer 1

3

Did you change DATABASE_TABLE after running your app at least once? If so you need to update your SQLite schema, the easiest way to do this is make DATABASE_VERSION = 2;. Also Android is very particular about the primary key, you should change COL_ID to "_id".

In the future, you must increment DATABASE_VERSION every time you change your CREATE_TABLE statement. Otherwise SQLite will not have the current information.

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

1 Comment

if this answer help you in solving your current issue then mark it answer for others help who is looking for same.Thanks.

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.