Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
687 views
in Technique[技术] by (71.8m points)

android - 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Increment DATABASE_VERSION so your onUpgrade() is called.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...