2013-08-31 73 views
-3

我有以下的數據庫連接代碼和操作:SQLite數據庫連接:表不存在

public class DBMessagesHandler 
{ 

    // All Static variables 
    // Database Version 
    private static final int DATABASE_VERSION = 1; 

    // Database Name 
    private static final String DATABASE_NAME = "Logindata"; 

    // Contacts table name 


    private static final String TABLE_MESSAGES = "Messages"; 

    // Contacts Table Columns names 
    private static final String KEY_ID = "id"; 
    private static final String KEY_MID = "mid"; 
    private static final String KEY_UID = "uid"; 
    private static final String KEY_MESSAGE = "message"; 
    private static final String KEY_NAME = "name"; 



    private DBHelper ourHelper; 
    private final Context ourContext; 
    private SQLiteDatabase ourDB; 

private static 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 "+TABLE_MESSAGES+ " ("+ 
        KEY_MID+" integer primary key autoincrement," +   
        KEY_UID+" integer NOT NULL, "+ 
        KEY_MESSAGE+" TEXT NOT NULL, "+ 
        ");"); 

     } 


     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int arg2) { 
      // TODO Auto-generated method stub 

      db.execSQL("drop table if exists"+ TABLE_MESSAGES); 
      onCreate(db); 
     } 



    } 


    public DBMessagesHandler (Context c) 
    { 
     ourContext=c; 

    } 

    public DBMessagesHandler open() throws SQLException 
    { 
     ourHelper=new DBHelper(ourContext); 
     ourDB=ourHelper.getWritableDatabase(); 
     return this;  
    } 

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

    // Add Messages 
    public long addMessage(int uid,String message) 
    { 
     ContentValues cv=new ContentValues(); 
     cv.put(KEY_UID, uid); 
     cv.put(KEY_MESSAGE, message); 

     return ourDB.insert(TABLE_MESSAGES, null, cv); 
    } 


    public int getCount(int uid) 
    { 
     String query = "select count(*) from "+TABLE_MESSAGES+ " WHERE uid=?"; 

     Cursor c = ourDB.rawQuery(query,null); 

     return c.getCount(); 
    } 


    // Add Messages 
    public void deleteMessage(int uid) 
    { 

     String ALTER_TBL ="delete from " + TABLE_MESSAGES + 
       " where "+KEY_ID+" in (select "+ KEY_ID +" from "+ TABLE_MESSAGES+" order by _id LIMIT 5) and "+KEY_UID+" = "+uid+";"; 

     ourDB.execSQL(ALTER_TBL); 
    } 

} 

我通過調用這個代碼如下:

DBMessagesHandler messageEntry=new DBMessagesHandler(Message.this); 
        messageEntry.open(); 
        int cnt=messageEntry.getCount(Integer.parseInt(uid)); 
        messageEntry.deleteMessage(Integer.parseInt(uid)); 
messageEntry.close(); 

,但我發現,代碼是達不到:

@Override 
      public void onCreate(SQLiteDatabase db) { 
       // TODO Auto-generated method stub 

       db.execSQL("create table "+TABLE_MESSAGES+ " ("+ 
         KEY_MID+" integer primary key autoincrement," +   
         KEY_UID+" integer NOT NULL, "+ 
         KEY_MESSAGE+" TEXT NOT NULL, "+ 
         ");"); 

因此,當我看到logcat,我發現錯誤信息不存在這樣的表存在。

請幫幫我。

首先,它要求:公共DBMessagesHandler(上下文C)

然後:公共DBMessagesHandler打開()拋出的SQLException

然後:公共DBHelper(上下文的背景下)

然後:公衆詮釋getCount將(INT UID)

logcat的:

08-31 15:43:14.634:I /數據庫(380):源碼返回:錯誤代碼= 1,MSG =沒有這樣的表:消息

+0

這數據庫版本ar你在用嗎? – Piyush

+0

請在這裏添加你的logcat。 –

+0

private static final int DATABASE_VERSION = 1; –

回答

1

我覺得你的代碼的OnCreate需要一些輕微的修改,我會後我的代碼,你看看我的代碼和編輯根據您的需要

@Override 
    public void onCreate(SQLiteDatabase db) { 
     // TODO Auto-generated method stub 
     String DATABASE_CREATE_PROJ = "CREATE TABLE " + DATABASE_TABLE_PROJ + "(" 
       + CATEGORY_COLUMN_ID + " integer primary key, " 
       + CATEGORY_COLUMN_TITLE + " text not null, " + CATEGORY_COLUMN_CONTENT + " text not null, " + CATEGORY_COLUMN_COUNT + " integer, " + CATEGORY_COLUMN_DATE + " integer);" ; 
       db.execSQL(DATABASE_CREATE_PROJ); 
} 

更改你的數據庫名稱,如果其要求,因爲如果添加任何表的手段,你的數據庫不會update.so對於這種情況下,你必須改變數據庫名,檢查和版本也

+0

但它沒有在調試器上創建方法 –

+0

你可以在你的代碼中這樣嘗試嗎,它是你的代碼在數據庫活性創建問題上創建的還是你的java文件活性創建的 –

+0

08-31 15:43:14。634:我/數據庫(380):sqlite返回:錯誤代碼= 1,msg =沒有這樣的表:消息 –

1

只需更新DATABSE_VERSION 3 ...

+0

加1票?驚喜.. !!它應該是一個評論。 – Andrain