SQLiteException:表消息在android

时间:2016-02-20 12:23:06

标签: android sqlite

我正在创建一个表名为message的SQLite数据库。创建表后,我添加了一个名为MUD的列。

它给我一个像这样的错误

android.database.sqlite.SQLiteException: table message has no column named MUD (code 1)
编译时

INSERT INTO message(MUD,FROM_ID,TO_ID,TO1,MSG_TYPE,FROM1,MSG_DATE,MSG_BODY,STATUs) VALUES (?,?,?,?,?,?,?,?,?)

这是我的database.java类: -

public class Database extends SQLiteOpenHelper{

    public Database(Context context) {
        super(context, "School", null, 10677);
        // TODO Auto-generated constructor stub
    }

    String r="CREATE TABLE login(sno INTEGER PRIMARY KEY, USER_ID VARCHAR, USERTYPE VARCHAR,MOBILE VARCHAR,SCHOOL VARCHAR,PASSWORD VARCHAR,DEVICE VARCHAR,NAME VARCHAR,DEVI VARCHAR)";
    String s="Create TABLE message(MESSAGEID integer primary key autoincrement ,TO_ID VARCHAR,FROM_ID VARCHAR,MSG_BODY VARCHAR,MSG_TYPE VARCHAR,MSG_DATE VARCHAR,FROM1 VARCHAR,TO1 VARCHAR,STATUS VARCHAR,MUD VARCHAR)";

    @Override
    public void onCreate(SQLiteDatabase tm) {
         tm.execSQL(r);
         tm.execSQL(s);
        }
    @Override
    public void onUpgrade(SQLiteDatabase tm, int OldVersion, int NewVersion) {
            tm.execSQL("DROP TABLE IF EXISTS login" );
            tm.execSQL("DROP TABLE IF EXISTS message" );
            onCreate(tm);
    }
    public void store(String user_id,String user_type,String mobile,String school,String pass,String device,String name,String devi)
    {
        SQLiteDatabase tm = this.getWritableDatabase();
        ContentValues values = new ContentValues();

        values.put("USERTYPE", user_type);
        values.put("USER_ID", user_id);
        values.put("MOBILE",mobile); 
        values.put("SCHOOL",school);
        values.put("PASSWORD", pass);
        values.put("DEVICE", device);
        values.put("NAME",name);
        values.put("DEVI",devi);
        tm.insert("login", null, values);
        tm.close(); // Closing database connection
    }
    public int getintval(String myq)
    {
        int Id=0;
           SQLiteDatabase db = this.getWritableDatabase();
            Cursor cursor = db.rawQuery(myq, null);


            if (cursor.moveToFirst()) {
                do {
                      Id  = cursor.getInt(0);



                } while (cursor.moveToNext());
            }
        return Id;
    }
    public void storeMessage(String to_id, String from_id,String msg_body,String msg_type,String msg_date,String from1,String to1,String status,String mud)
    {
         SQLiteDatabase tm = this.getWritableDatabase();
            ContentValues values = new ContentValues();
          //  values.put("MESSAGEID",msgid);
            values.put("TO_ID", to_id);
            values.put("FROM_ID", from_id);
            values.put("MSG_BODY",msg_body); 
            values.put("MSG_TYPE",msg_type);
            values.put("MSG_DATE",msg_date);
            values.put("FROM1",from1);
            values.put("TO1",to1);
            values.put("STATUs", status);
            values.put("MUD",mud);
            tm.insert("message", null, values);
            tm.close();
    }
    public void storeMessageNew(String to_id, String from_id,String msg_body,String msg_type,String msg_date,String status)
    {
        SQLiteDatabase tm = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("TO_ID", to_id);
        values.put("FROM_ID", from_id);
        values.put("MSG_BODY",msg_body);
        values.put("MSG_TYPE",msg_type);
        values.put("MSG_DATE",msg_date);
        values.put("STATUs", status);
        tm.insert("message", null, values);
        tm.close();
    }

    // Updating single contact

public void updateMessageBody(int msg_id, String msg_body){
     SQLiteDatabase db = this.getWritableDatabase();

      ContentValues values = new ContentValues();
      values.put("MSG_BODY", msg_body);

      db.update("message", values, "MESSAGEID"+"="+msg_id, null);
     }
     public String [] check(String school)
        {
            SQLiteDatabase tm = this.getReadableDatabase();
                Cursor cursor = tm.query("login", new String[] {"SCHOOL","DEVI"},"SCHOOL=?",new String[] { String.valueOf(school) }, null, null, null, null);
            if (cursor != null)
                cursor.moveToFirst();
         return   new String[]{cursor.getString(1)};

        }



         public String getSchool_id(String device_id){
             String Id="";
               // String Table_Name="login";

             String selectQuery = "SELECT  * FROM login where DEVI= '"+device_id+"'";

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


                if (cursor.moveToFirst()) {
                    do {
                          Id  = cursor.getString(4);



                    } while (cursor.moveToNext());
                }

                return Id;
            }



         public String getAllData(String device_id){
             String Id="";
               // String Table_Name="login";

             String selectQuery = "SELECT  * FROM login where DEVICE= '"+device_id+"'";

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


                if (cursor.moveToFirst()) {
                    do {
                          Id  = cursor.getString(0);
                    } while (cursor.moveToNext());
                }

                return Id;
            }


         public void deleteMsg(String msg_id)
         {
             Log.d("MessageID", msg_id);
             String selectQuery = "Delete FROM message where MESSAGEID= '"+msg_id+"'";

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




         }


         public List<FetchData> getAllContacts() {
             Cursor cursor;
                List<FetchData> contactList = new ArrayList<FetchData>();
                // Select All Query
                String selectQuery = "SELECT  * FROM login ";
                SQLiteDatabase db = this.getWritableDatabase();
                cursor = db.rawQuery(selectQuery, null);
                {
                    if (cursor.moveToFirst()) {
                        do {
                            FetchData fetchdata = new FetchData();
                            //contact.setID(Integer.parseInt(cursor.getString(0)));
                            fetchdata.setID(cursor.getInt(1));
                            fetchdata.setType(cursor.getString(2));
                            fetchdata.setMobile(cursor.getString(3));
                            fetchdata.setSchool_Id(cursor.getString(4));
                            fetchdata.setPassword(cursor.getString(5));
                            fetchdata.set_reg_id(cursor.getString(6));
                            fetchdata.set_name(cursor.getString(7));
                            contactList.add(fetchdata);
                        } while (cursor.moveToNext());
                    }
                    cursor.close();
                }
                return contactList;
            }
         public Cursor getData(String myq)
         { Cursor cursor;
             SQLiteDatabase db = this.getWritableDatabase();
                cursor = db.rawQuery(myq, null);
                return cursor;
         }




}   

我正在使用它

db.storeMessage(to_id, from_id + "", message1, "text", localTime, "ST", "F", "R", roomId);

任何人都可以帮助我。

1 个答案:

答案 0 :(得分:1)

每次更改架构时都应该增加数据库版本

public Database(Context context) {
    super(context, "School", null, 10678);
    // TODO Auto-generated constructor stub
}