Himani
Himani

Reputation: 227

how to add a new table in existing database

i m trying to add a new table in the database,but whenever i run my application,it give me an error tablename doesnot exist,how to add a table that have samoe fields and function to get the data from the user ,that he write in the activity's edittext.

   package com.example.smscampaign;

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

    import org.w3c.dom.Comment;

    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.SQLiteDatabase.CursorFactory;
    import android.database.sqlite.SQLiteOpenHelper;

    public class DatabaseHelp {

        public static final String KEY_ROWID = "_id";
        public static final String KEY_NAME = "person_name";
        public static final String KEY_SCALE = "scale_person";
        public static final String KEY_CONTACTS = "Contacts_person";
        public static final String KEY_To_ROWID = "_id";
        public static final String KEY_To_NAME = "person_name";
        public static final String KEY_To_SCALE = "scale_person";
        public static final String KEY_To_CONTACTS = "Contacts_person";

        private static final String DATABASE_NAME = "Himani";
        static final String DATABASE_TABLE = "peopleTable";
        private static final String TABLE_ADD_MESSAGE = "Add_Message";
        private static final int DATABASE_VERSION = 1;

        private DbHelper ourHepler;
        private final Context ourContext;
        SQLiteDatabase ourDatabase;

        public class DbHelper extends SQLiteOpenHelper {

            public DbHelper(Context context) {
                super(context, DATABASE_NAME, null, DATABASE_VERSION);
                // TODO Auto-generated constructor stub
            }

            @Override
            public void onCreate(SQLiteDatabase db) {
                // TODO Auto-generated method stub
                db.execSQL( "CREATE TABLE " + DATABASE_TABLE + " (" +
                        KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT,  " +
                        KEY_NAME + " TEXT NOT NULL, " +
                        KEY_SCALE + " TEXT NOT NULL ,"  + KEY_CONTACTS + ")"
                        );
                String CREATE_ADD_MESSAGE= "CREATE TABLE " + TABLE_ADD_MESSAGE +
                         "(" + KEY_To_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT,  " +
                         KEY_To_NAME + " TEXT NOT NULL, " +
                         KEY_To_SCALE + " TEXT NOT NULL ,"  + KEY_To_CONTACTS + ")" ;
                                    db.execSQL(CREATE_ADD_MESSAGE);

                }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                // TODO Auto-generated method stub
                db.execSQL("DROP TABLE IF EXISTS" + DATABASE_TABLE);
                db.execSQL("DROP TABLE IF EXISTS" + TABLE_ADD_MESSAGE);

                onCreate(db);
            }

        }

        public DatabaseHelp(Context c) {
            ourContext = c;
        }

        public DatabaseHelp open() throws SQLException {
            ourHepler = new DbHelper(ourContext);
            ourDatabase = ourHepler.getWritableDatabase();
            return this;
        }

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

        public long entryCreate(String name, String scale, String contacts) {
            // TODO Auto-generated method stub

            ContentValues cv = new ContentValues();
            cv.put(KEY_NAME, name);
            cv.put(KEY_SCALE, scale);
            cv.put(KEY_CONTACTS, contacts);
            return ourDatabase.insert(DATABASE_TABLE, null, cv);

        }

        public long entryCreate1(String name, String scale, String contacts) {
            // TODO Auto-generated method stub

            ContentValues cv = new ContentValues();
            cv.put(KEY_To_NAME, name);
            cv.put(KEY_To_SCALE, scale);
            cv.put(KEY_To_CONTACTS, contacts);
            return ourDatabase.insert(TABLE_ADD_MESSAGE, null, cv);

        }

        public ArrayList<String> getData() {

            String[] col = new String[] { KEY_ROWID, KEY_NAME, KEY_SCALE,
                    KEY_CONTACTS };
            Cursor c = ourDatabase.query(DATABASE_TABLE, col, null, null, null,
                    null, null);
            String run = "";
            int iRow = c.getColumnIndex(KEY_ROWID);
            int iName = c.getColumnIndex(KEY_NAME);
            int iScale = c.getColumnIndex(KEY_SCALE);
            int iMessage = c.getColumnIndex(KEY_CONTACTS);
            ArrayList<String> newList = new ArrayList<String>();
            for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
                newList.add(c.getString(iName));
            }

            return newList;
        }

        public ArrayList<String> getData1() {

            String[] col = new String[] { KEY_To_ROWID, KEY_To_NAME, KEY_To_SCALE,
                    KEY_To_CONTACTS };
            Cursor c = ourDatabase.query(TABLE_ADD_MESSAGE, col, null, null, null,
                    null, null);
            String run = "";
            int iRow = c.getColumnIndex(KEY_To_ROWID);
            int iName = c.getColumnIndex(KEY_To_NAME);
            int iScale = c.getColumnIndex(KEY_To_SCALE);
            int iMessage = c.getColumnIndex(KEY_To_CONTACTS);
            ArrayList<String> newList = new ArrayList<String>();
            for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
                newList.add(c.getString(iName));
            }

            return newList;
        }

        public Cursor fetchChildren(String KEY_) {
            Cursor c = ourDatabase.rawQuery("SELECT * FROM " + DATABASE_TABLE
                    + " WHERE person_name = ?", new String[] { KEY_ });
            return c;
        }

        public Cursor fetchChildren1(String KEY_) {
            Cursor c = ourDatabase.rawQuery("SELECT * FROM " + DATABASE_TABLE
                    + " WHERE person_name = ?", new String[] { KEY_ });
            return c;
        }

        public String getScale(long l) {
            // TODO Auto-generated method stub
            String[] col = new String[] { KEY_ROWID, KEY_NAME, KEY_SCALE,
                    KEY_CONTACTS };
            Cursor c = ourDatabase.query(DATABASE_TABLE, col, KEY_ROWID + "-" + l,
                    null, null, null, null);

            if (c != null) {
                c.moveToFirst();
                String scale = c.getString(2);
                return scale;
            }
            return null;
        }

        public String getScale1(long l) {
            // TODO Auto-generated method stub
            String[] col = new String[] { KEY_To_ROWID, KEY_To_NAME, KEY_To_SCALE,
                    KEY_To_CONTACTS };
            Cursor c = ourDatabase.query(TABLE_ADD_MESSAGE, col, KEY_To_ROWID + "-"
                    + l, null, null, null, null);

            if (c != null) {
                c.moveToFirst();
                String scale = c.getString(2);
                return scale;
            }
            return null;
        }

        public String getName(long l) {
            // TODO Auto-generated method stub
            String[] col = new String[] { KEY_ROWID, KEY_NAME, KEY_SCALE,
                    KEY_CONTACTS };
            Cursor c = ourDatabase.query(DATABASE_TABLE, col, KEY_ROWID + "-" + l,
                    null, null, null, null);

            if (c != null) {
                c.moveToFirst();
                String name = c.getString(1);
                return name;

            }
            return null;
        }

        public String getName1(long l) {
            // TODO Auto-generated method stub
            String[] col = new String[] { KEY_To_ROWID, KEY_To_NAME, KEY_SCALE,
                    KEY_To_CONTACTS };
            Cursor c = ourDatabase.query(TABLE_ADD_MESSAGE, col, KEY_To_ROWID + "-"
                    + l, null, null, null, null);

            if (c != null) {
                c.moveToFirst();
                String name = c.getString(1);
                return name;

            }
            return null;
        }

        public String getContacts(long l) {
            // TODO Auto-generated method stub
            String[] col = new String[] { KEY_ROWID, KEY_NAME, KEY_SCALE,
                    KEY_CONTACTS };
            Cursor c = ourDatabase.query(DATABASE_TABLE, col, KEY_ROWID + "-" + l,
                    null, null, null, null);

            if (c != null) {
                c.moveToFirst();
                String contacts = c.getString(3);
                return contacts;

            }
            return null;
        }

        public String getContacts1(long l) {
            // TODO Auto-generated method stub
            String[] col = new String[] { KEY_To_ROWID, KEY_To_NAME, KEY_To_SCALE,
                    KEY_To_CONTACTS };
            Cursor c = ourDatabase.query(TABLE_ADD_MESSAGE, col, KEY_To_ROWID + "-"
                    + l, null, null, null, null);

            if (c != null) {
                c.moveToFirst();
                String contacts = c.getString(3);
                return contacts;

            }
            return null;
        }

        public void updateEntry(long lt, String mName, String mScale,
                String mContatcs) {
            // TODO Auto-generated method stub
            ContentValues cvUpdate = new ContentValues();
            cvUpdate.put(KEY_NAME, mName);
            cvUpdate.put(KEY_SCALE, mScale);
            cvUpdate.put(KEY_CONTACTS, mContatcs);
            ourDatabase
                    .update(DATABASE_TABLE, cvUpdate, KEY_ROWID + "-" + lt, null);
        }

        public void updateEntry1(long lt, String mName, String mScale,
                String mContatcs) {
            // TODO Auto-generated method stub
            ContentValues cvUpdate = new ContentValues();
            cvUpdate.put(KEY_To_NAME, mName);
            cvUpdate.put(KEY_To_SCALE, mScale);
            cvUpdate.put(KEY_To_CONTACTS, mContatcs);
            ourDatabase.update(TABLE_ADD_MESSAGE, cvUpdate,
                    KEY_To_ROWID + "-" + lt, null);
        }

        public void deleteEntry1(long ltt) throws SQLException {
            // TODO Auto-generated method stub
            ourDatabase.delete(TABLE_ADD_MESSAGE, KEY_To_ROWID + "=" + ltt, null);

        }

        public void deleteEntry(long ltt) throws SQLException {
            // TODO Auto-generated method stub
            ourDatabase.delete(DATABASE_TABLE, KEY_ROWID + "=" + ltt, null);

        }
    }

always get this error

> 03-19 16:46:52.396: E/AndroidRuntime(10089):
> java.lang.RuntimeException: Unable to start activity
> ComponentInfo{com.example.smscampaign/com.example.smscampaign.Campaign_Details}:
> android.database.sqlite.SQLiteException: no such table: Add_Message
> (code 1): , while compiling: SELECT _id, name, message, Contacts FROM
> Add_Message

Upvotes: 2

Views: 4409

Answers (4)

Mihai Moldoveanu
Mihai Moldoveanu

Reputation: 1

In case there are others that don't want their entire database to be deleted and rewritten without anything in it when creating a new table, there is another method of SQLOpenHelper that you can override: onOpen. You can INSERT TABLE IF NOT EXISTS there, and it will check if the table already exists. Otherwise it will create a new one. Maybe it's not the best solution, but it's the only one that can add a table to the database without destroying data and creating empty tables in it.

Upvotes: 0

Atul O Holic
Atul O Holic

Reputation: 6792

onCreate method of your DbHelper is only called once when you make a call to getWritableDatabase() or getReadableDatabase() for the very first time. Which already happened when you created your first table (DATABASE_TABLE) but is not called now and hence you are not able to create the new table (TABLE_ADD_MESSAGE).

As a result when accessing the TABLE_ADD_MESSAGE you getting the error. You can verify this yourself by putting a log statement there and you will notice its not getting executed.

Solution to this is what @laalto suggested. :)

Upvotes: 1

Sachin Shelke
Sachin Shelke

Reputation: 449

Change your database version to 2 then only onUpgrade getting called and code will be executed.

 private static final int DATABASE_VERSION = 2;

Upvotes: 2

laalto
laalto

Reputation: 152927

Increment DATABASE_VERSION so your onUpgrade() is called.

For more information, see When is SQLiteOpenHelper onCreate() / onUpgrade() run?

Upvotes: 4

Related Questions