2011-12-08 53 views
0

戳我有一個錯誤:安卓:對sqlite的

12-08 12:05:20.264: E/AndroidRuntime(21782): Caused by: android.database.sqlite.SQLiteException: no such column: details: , while compiling: SELECT _id, recipient, message, details FROM notes 

我在做這一切錯了嗎?我的數據庫似乎不工作,我只是想添加一個時間戳。任何人都可以幫助我如何在sqlite上添加時間戳?這是我在數據庫上的代碼:

public class MessagesDBAdapter { 

    public static final String KEY_RECIPIENT = "recipient"; 
    public static final String KEY_MESSAGE = "message"; 
    public static final String KEY_DETAILS = "details"; 
    public static final String KEY_ROWID = "_id"; 

    private static final String TAG = "MessagesDBAdapter"; 
    private DatabaseHelper mDbHelper; 
    private SQLiteDatabase mDb; 

    //Database creation sql statement 

    private static final String DATABASE_CREATE = 
      "create table notes (" + KEY_ROWID + " integer primary key autoincrement, " 
     + KEY_RECIPIENT + " text not null, " + KEY_MESSAGE + " text not null, " + KEY_DETAILS + "text not null);"; 

    private static final String DATABASE_NAME = "data"; 
    private static final String DATABASE_TABLE = "notes"; 
    private static final int DATABASE_VERSION = 2; 

    private final Context mCtx; 

    private static class DatabaseHelper extends SQLiteOpenHelper { 

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

     @Override 
     public void onCreate(SQLiteDatabase db) { 
      db.execSQL(DATABASE_CREATE); 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      Log.w(TAG, "Upgrading database from version " + oldVersion + " to " 
        + newVersion + ", which will destroy all old data"); 
      db.execSQL("DROP TABLE IF EXISTS notes"); 
      onCreate(db); 
     } 
    } 

    /** 
    * Constructor - takes the context to allow the database to be 
    * opened/created 
    * 
    * @param ctx the Context within which to work 
    */ 
    public MessagesDBAdapter(Context ctx) { 
     this.mCtx = ctx; 
    } 

    public MessagesDBAdapter open() throws SQLException { 
     mDbHelper = new DatabaseHelper(mCtx); 
     mDb = mDbHelper.getWritableDatabase(); 
     return this; 
    } 

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

    public long createNote(String phoneNo, String message, String details) { 
     ContentValues initialValues = new ContentValues(); 
     initialValues.put(KEY_RECIPIENT, phoneNo); 
     initialValues.put(KEY_MESSAGE, message); 
     initialValues.put(KEY_DETAILS, details); 

     open(); 

     return mDb.insert(DATABASE_TABLE, null, initialValues); 
    } 

    public boolean deleteNote(long rowId) { 

     return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0; 
    } 

    public Cursor fetchAllNotes() { 

     return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_RECIPIENT, 
       KEY_MESSAGE, KEY_DETAILS }, null, null, null, null, null); 
    } 

    public Cursor fetchNote(long rowId) throws SQLException { 

     Cursor mCursor = 

      mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, 
        KEY_RECIPIENT, KEY_MESSAGE, KEY_DETAILS}, KEY_ROWID + "=" + rowId, null, 
        null, null, null, null); 
     if (mCursor != null) { 
      mCursor.moveToFirst(); 
     } 
     return mCursor; 

    } 

    public boolean updateNote(long rowId, String phoneNo, String message, String details) { 
     ContentValues args = new ContentValues(); 
     args.put(KEY_RECIPIENT, phoneNo); 
     args.put(KEY_MESSAGE, message); 
     args.put(KEY_DETAILS, details); 

     return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0; 
    } 
} 

任何人都可以幫助我解決這個問題。

回答

1
+ KEY_DETAILS + "text not null);"; 

應該

+ KEY_DETAILS + " text not null);"; 

通知 「文本」 前的空格。 :)

+0

你錯誤地在你的分貝中創建了「detailstext」列。 – tacone

+0

這是哪一部分? – Kev

+0

將DATABASE_VERSION更新到更大的版本(3?),以確保您的表被刪除並重新創建:) – tacone