2011-08-10 11 views
0

這是我CallEventAdapter: ` 公共類CallEventAdapter {安卓:我不能創建一個數據庫,我得到錯誤:公共無效的onCreate(SQLiteDatabase DB)

public static final String  KEY_ID    = "id"; 

public static final String  KEY_LABEL   = "label"; 

public static final String  KEY_CONTENT   = "content"; 

private static final String  TAG     = "CallAdapter"; 

private static final String  DATABASE_NAME  = "call_Event"; 

private static final String  DATABASE_TABLE  = "calls"; 

private static final int  DATABASE_VERSION = 1; 

private static final String  DATABASE_CREATE  = "create table if not exists calls (id integer primary key, label text not null, content text not nul);"; 

// private static String DATABASE_PATH = 
// "/data/data/fr.intuitiv.app.activity/databases/"; // for now 

private final Context   context; 

private final DatabaseHelper DBHelper; 

private SQLiteDatabase   db; 

public CallEventAdapter(Context ctx) { 
    this.context = ctx; 
    DBHelper = new DatabaseHelper(context); 
} 

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 calEvent"); 
     onCreate(db); 
    } 
} 

// ---opens the database--- 
public CallEventAdapter open() throws SQLException { 
    db = DBHelper.getWritableDatabase(); 
    return this; 
} 

// ---closes the database--- 
public void close() { 
    DBHelper.close(); 
} 

// ---insert a CalEvent into the database--- 
public long insertCallEvent(long id, String label, String content) { 
    ContentValues initialValues = new ContentValues(); 
    initialValues.put(KEY_ID, id); 
    initialValues.put(KEY_LABEL, label); 
    initialValues.put(KEY_CONTENT, content); 
    return db.insert(DATABASE_TABLE, null, initialValues); 
} 

// ---deletes a particular CallEvent--- 
public boolean deleteCallEvent(long id) { 
    return db.delete(DATABASE_TABLE, KEY_ID + "=" + id, null) > 0; 
} 

// ---retrieves all CallEvents--- 
public Cursor getAllCallEvents() { 
    return db.query(DATABASE_TABLE, new String[] { KEY_ID, KEY_LABEL, KEY_CONTENT }, null, null, null, null, null); 
} 

// ---retrieves a particular CallEvent--- 
public Cursor getCallEvent(long id) throws SQLException { 
    Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] { KEY_ID, KEY_LABEL, KEY_CONTENT }, KEY_ID + "=" + id, null, null, null, null, null); 
    if(mCursor != null) { 
     mCursor.moveToFirst(); 
    } 
    return mCursor; 
} 

// ---updates a CallEvent--- 
public boolean updateCallEvent(long id, String label, String content) { 
    ContentValues args = new ContentValues(); 
    args.put(KEY_LABEL, label); 
    args.put(KEY_CONTENT, content); 
    return db.update(DATABASE_TABLE, args, KEY_ID + "=" + id, null) > 0; 
} 

}

this is my CallListActivity: 公共類CallListActivity擴展活動{

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.call_list_events); 
    CallEventAdapter db = new CallEventAdapter(this); 
    // ---add 2 calEvents--- 
    db.open(); 
    long id; 
    id = db.insertCalEvent(3, "0470285818", "test 1"); 
    id = db.insertCalEvent(4, "047017661X", "test2"); 
    db.close(); 
} 

} `

+1

是什麼在logcat的? – Im0rtality

回答

1

檢查這一行:

private static final String  DATABASE_CREATE  = "create table if not exists calls (id integer primary key, label text not null, content text not null);"; 

單詞 「空」 的結尾是不完整的

+0

是的,你是對的,現在我得到:id = -1,在Activity中,其中:id = db.insert .... 這意味着我還沒有數據庫? – Nayden

+0

可以發佈出現的logcat錯誤,以便識別錯誤更容易。 – SamSPICA

+0

如何從我的數據庫中獲取表格?檢查一切正常嗎? – Nayden

相關問題