2012-08-08 17 views
0

我在關於在一個數據庫中存儲多個表的這個示例Multiple Table SQLite DB Adapter(s) in Android?將多個表存儲在一個SQLitedatabase中的問題

我創建了4個DBAdapter(DBAdapter,CarsDBAdapter,BoatsDBAdapter和CyclesDBAdapter)。但在DBAdapter之一中,我已經在db適配器中創建了3個表。然後,我爲其他3 db適配器創建了3個非DBadaper類。表中沒有插入到數據庫中,我不確定哪個DBAdapter我應該在非dbadapter類中聲明。

我需要幫助。我希望提供任何幫助。

下面是我輸入的代碼。

DBAdapter.java

public class DBAdapter 
{ 
    public static final String DATABASE_NAME = "stuffIOwn"; 
    public static final int DATABASE_VERSION = 2; 

    private static final String CREATE_TABLE_CARS = "create table cars(_id integer primary key autoincrement, " //$NON-NLS-1$ 
      +CarsDBAdapter.NAME+ " TEXT," //$NON-NLS-1$ 
      +CarsDBAdapter.MODEL+ " TEXT," //$NON-NLS-1$ 
      +CarsDBAdapter.YEAR+ " TEXT" +");"; //$NON-NLS-1$ 

    private static final String CREATE_TABLE_BOATS = "create table boats(_id integer primary key autoincrement, " //$NON-NLS-1$ 
      +BoatsDBAdapter.NAME+ " TEXT," //$NON-NLS-1$ 
      +BoatsDBAdapter.MODEL+ " TEXT," //$NON-NLS-1$ 
      +BoatsDBAdapter.YEAR+ " TEXT" +");"; //$NON-NLS-1$ 

    private static final String CREATE_TABLE_CYCLES = "create table cycles(_id integer primary key autoincrement, " //$NON-NLS-1$ 
      +CyclesDBAdapter.NAME+ " TEXT," //$NON-NLS-1$ 
      +CyclesDBAdapter.MODEL+ " TEXT," //$NON-NLS-1$ 
      +CyclesDBAdapter.YEAR+ " TEXT" +");"; //$NON-NLS-1$ 

    private final Context context; 
    private DatabaseHelper DBHelper; 
    private SQLiteDatabase db; 

    public DBAdapter(Context ctx)  
    {   
     this.context = ctx;   
     this.DBHelper = new DatabaseHelper(this.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(CREATE_TABLE_CARS);    
      db.execSQL(CREATE_TABLE_BOATS);    
      db.execSQL(CREATE_TABLE_CYCLES);     
     }   
     @Override   
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)   
     {        
      // Adding any table mods to this guy here   
     }  
    }//end DatabaseHelper class 

    public DBAdapter open() throws SQLException  
    {   
    this.db = this.DBHelper.getWritableDatabase();   
    return this;  
    }  
    /**  
    * close the db 
    *return type: void  
    */  
    public void close()  
    {   
     this.DBHelper.close();  
    } 

} 

CarsDBAdapter.java

public class CarsDBAdapter 
{ 
    public static final String ROW_ID = "_id"; 
    public static final String NAME = "name"; 
    public static final String MODEL = "model"; 
    public static final String YEAR = "year"; 

    private static final String DATABASE_TABLE = "cars"; 

    private DatabaseHelper mDbHelper; 
    private SQLiteDatabase mDb; 

    private final Context mCtx; 

    private static class DatabaseHelper extends SQLiteOpenHelper 
    { 
     DatabaseHelper(Context context) 
     { 
      super(context, DBAdapter.DATABASE_NAME, null, DBAdapter.DATABASE_VERSION);   
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) 
     {   
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
     {   
     } 
    }// end DatabaseHelper class 

    public CarsDBAdapter(Context ctx) 
    { 
     this.mCtx = ctx; 
    } 

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

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

    public long insertCars(String name, String model, String year) 
    { 
     ContentValues initialValues = new ContentValues(); 
     initialValues.put(NAME, name); 
     initialValues.put(MODEL, model); 
     initialValues.put(YEAR, year); 
     return mDb.insert(DATABASE_TABLE, null, initialValues); 
    } 

    public long create(String name, String model, String year) 
    { 
     ContentValues initialValues = new ContentValues(); 

     initialValues.put(NAME, name); 
     initialValues.put(MODEL, model); 
     initialValues.put(YEAR, year); 
     return this.mDb.insert(DATABASE_TABLE, null, initialValues); 
    } 

    public boolean deleteCar(long rowId) 
    {   
     return this.mDb.delete(DATABASE_TABLE, ROW_ID + "=" + rowId, null) > 0; //$NON-NLS-1$  
    } 

    public Cursor getAllCars() 
    {   
     return this.mDb.query(DATABASE_TABLE, new String[] { ROW_ID, NAME, MODEL, YEAR }, null, null, null, null, null);  
    } 

    public Cursor getCar(long rowId) throws SQLException 
    { 
     Cursor mCursor = this.mDb.query(true, DATABASE_TABLE, new String[] {ROW_ID, NAME, MODEL, YEAR}, ROW_ID + "=" + rowId, 
       null, null, null, null, null); 

     if(mCursor != null) 
     { 
      mCursor.moveToFirst(); 
     } 

     return mCursor; 
    } 

    public boolean updateCar(long rowId, String name, String model, String year) 
    { 
     ContentValues args = new ContentValues(); 

     args.put(NAME, name); 
     args.put(MODEL, model); 
     args.put(YEAR, year); 
     return this.mDb.update(DATABASE_TABLE, args, ROW_ID + "=" + rowId, null) >0; 
    } 

}//end CarsDBAdapter class 

沒有爲BoatsDBAdapter和CyclesDBAdapter類,但插入,更新一樣,刪除方法名稱不同。 這些都是非將對DBAdapter類

Cars.java

public class Cars extends Activity 
{ 
    final Context context = this; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.cars); 

     CarsDBAdapter carsDB = new CarsDBAdapter(this); 

     //DBAdapter dbA = new DBAdapter(this); 

     carsDB.open(); 
     long id; 
     id = carsDB.insertCars("Mercedes", "MERCDS", "2000"); 
     id = carsDB.insertCars("BMW", "BMWTO", "1999"); 
     carsDB.close(); 

     carsDB.open(); 

     Cursor c = carsDB.getAllCars(); 
     if (c.moveToFirst()) 
     { 
      do 
      { 
       DisplayContact(c); 
      } while (c.moveToNext()); 
     } 

     carsDB.close(); 

     Button btnMenu = (Button) findViewById(R.id.btnMenu); 
     btnMenu.setOnClickListener(new View.OnClickListener() 
      { 

       @Override 
       public void onClick(View v) 
       { 
        Intent menuIntent = new Intent(context, VehiclesMenu.class); 
        startActivity(menuIntent); 

       } 
      }); 
    }//end onCreate() 

    public void DisplayContact(Cursor c) 
    { 
     Toast.makeText(
       this, 
       "id: " + c.getString(0) + "\n" + "Name: " + c.getString(1) 
         + "\n" + "Model: " + c.getString(2) + "\n" + "Year: " + c.getString(3), Toast.LENGTH_LONG) 
       .show(); 
    } 
} 

我沒有爲Boats.java和Cycles.java相同,只是我Cycles.java

聲明BoatsDBAdapter在Boats.java和CyclesDBAdapter

在Boats.java

BoatsDBAdapter boatsDB = new BoatsDBAdapter(this); 

     boatsDB.open(); 
     long id; 
     id = boatsDB.insertBoats("Jet-ski", "ERTYU", "2002"); 
     id = boatsDB.insertBoats("Motor-boat", "MBCTY", "2003"); 
     boatsDB.close(); 

     boatsDB.open(); 

     Cursor c = boatsDB.getAllBoats(); 
     if (c.moveToFirst()) 
     { 
      do 
      { 
       DisplayContact(c); 
      } while (c.moveToNext()); 
     } 

     boatsDB.close(); 

和Cycles.java

CyclesDBAdapter cyclesDB = new CyclesDBAdapter(this); 

     cyclesDB.open(); 
     long id; 
     id = cyclesDB.insertCycles("Mountain-bike", "ADCFG", "2004"); 
     id = cyclesDB.insertCycles("Dirt-bike", "VBNGH", "2006"); 
     cyclesDB.close(); 

     cyclesDB.open(); 
     Cursor c = cyclesDB.getAllCycles(); 
     if (c.moveToFirst()) 
     { 
      do 
      { 
       DisplayContact(c); 
      } while (c.moveToNext()); 
     } 
     cyclesDB.close(); 

UPDATE! 我已經對我的代碼進行了一些更新。我已將插入代碼添加到CarsDBAdapter中。但是插入代碼不在上面鏈接的示例中。我已經刪除了BoatsDBAdapter,CyclesDBAdapter,Boats和Cycles類,因爲代碼類似於CarsDBAdapter和Cars類。

回答

0

我想有三個適配器將類似於以下(我只在一個將作爲演示,但這個想法是每個

public class CarsDBAdapter { 
    public static final String ROW_ID = "_id"; 
    public static final String NAME = "name"; 
    public static final String MODEL = "model"; 
    public static final String YEAR = "year"; 

    private static final String DATABASE_TABLE = "cars"; 

    private DatabaseHelper mDbHelper; 
    private SQLiteDatabase mDb; 

    private final Context mCtx; 

    private static class DatabaseHelper extends SQLiteOpenHelper { 

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

     @Override 
     public void onCreate(SQLiteDatabase db) { 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     } 
    } 

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

    /** 
    * Open the cars database. If it cannot be opened, try to create a new 
    * instance of the database. If it cannot be created, throw an exception to 
    * signal the failure 
    * 
    * @return this (self reference, allowing this to be chained in an 
    *   initialization call) 
    * @throws SQLException 
    *    if the database could be neither opened or created 
    */ 
    public CarsDBAdapter open() throws SQLException { 
     this.mDbHelper = new DatabaseHelper(this.mCtx); 
     this.mDb = this.mDbHelper.getWritableDatabase(); 
     return this; 
    } 

    /** 
    * close return type: void 
    */ 
    public void close() { 
     this.mDbHelper.close(); 
    } 

    /** 
    * Create a new car. If the car is successfully created return the new 
    * rowId for that car, otherwise return a -1 to indicate failure. 
    * 
    * @param name 
    * @param model 
    * @param year 
    * @return rowId or -1 if failed 
    */ 
    public long createCar(String name, String model, String year){ 
     ContentValues initialValues = new ContentValues(); 
     initialValues.put(NAME, name); 
     initialValues.put(MODEL, model); 
     initialValues.put(YEAR, year); 
     return this.mDb.insert(DATABASE_TABLE, null, initialValues); 
    } 

    /** 
    * Delete the car with the given rowId 
    * 
    * @param rowId 
    * @return true if deleted, false otherwise 
    */ 
    public boolean deleteCar(long rowId) { 

     return this.mDb.delete(DATABASE_TABLE, ROW_ID + "=" + rowId, null) > 0; //$NON-NLS-1$ 
    } 

    /** 
    * Return a Cursor over the list of all cars in the database 
    * 
    * @return Cursor over all cars 
    */ 
    public Cursor getAllCars() { 

     return this.mDb.query(DATABASE_TABLE, new String[] { ROW_ID, 
       NAME, MODEL, YEAR }, null, null, null, null, null); 
    } 

    /** 
    * Return a Cursor positioned at the car that matches the given rowId 
    * @param rowId 
    * @return Cursor positioned to matching car, if found 
    * @throws SQLException if car could not be found/retrieved 
    */ 
    public Cursor getCar(long rowId) throws SQLException { 

     Cursor mCursor = 

     this.mDb.query(true, DATABASE_TABLE, new String[] { ROW_ID, NAME, 
       MODEL, YEAR}, ROW_ID + "=" + rowId, null, null, null, null, null); 
     if (mCursor != null) { 
      mCursor.moveToFirst(); 
     } 
     return mCursor; 
    } 

    /** 
    * Update the car. 
    * 
    * @param rowId 
    * @param name 
    * @param model 
    * @param year 
    * @return true if the note was successfully updated, false otherwise 
    */ 
    public boolean updateCar(long rowId, String name, String model, 
      String year){ 
     ContentValues args = new ContentValues(); 
     args.put(NAME, name); 
     args.put(MODEL, model); 
     args.put(YEAR, year); 

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

} 

所以我的主要將對DBAdapter相同(它負責在一個單一的數據庫創建我所有的表)看起來像這樣

public class DBAdapter { 

    public static final String DATABASE_NAME = "stuffIOwn"; //$NON-NLS-1$ 

    public static final int DATABASE_VERSION = 1; 

    private static final String CREATE_TABLE_CARS = 
     "create table cars (_id integer primary key autoincrement, " //$NON-NLS-1$ 
    + CarsDBAdapter.NAME+ " TEXT," //$NON-NLS-1$ 
    + CarsDBAdapter.MODEL+ " TEXT," //$NON-NLS-1$ 
    + CarsDBAdapter.YEAR+ " TEXT" + ");"; //$NON-NLS-1$ //$NON-NLS-2$ 

    private static final String CREATE_TABLE_BOATS = "create table boats (_id integer primary key autoincrement, " //$NON-NLS-1$ 
    +BoatsDBAdapter.NAME+" TEXT," //$NON-NLS-1$ 
    +BoatsDBAdapter.MODEL+" TEXT," //$NON-NLS-1$ 
    +BoatsDBAdapter.YEAR+" TEXT"+ ");"; //$NON-NLS-1$ //$NON-NLS-2$ 

     private static final String CREATE_TABLE_CYCLES = "create table cycles (_id integer primary key autoincrement, " //$NON-NLS-1$ 
    +CyclesDBAdapter.NAME+" TEXT," //$NON-NLS-1$ 
    +CyclesDBAdapter.MODEL+" TEXT," //$NON-NLS-1$ 
    +CyclesDBAdapter.YEAR+" TEXT"+ ");"; //$NON-NLS-1$ //$NON-NLS-2$ 


    private final Context context; 
    private DatabaseHelper DBHelper; 
    private SQLiteDatabase db; 

    /** 
    * Constructor 
    * @param ctx 
    */ 
    public DBAdapter(Context ctx) 
    { 
     this.context = ctx; 
     this.DBHelper = new DatabaseHelper(this.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(CREATE_TABLE_CARS); 
      db.execSQL(CREATE_TABLE_BOATS); 
      db.execSQL(CREATE_TABLE_CYCLES);   
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, 
     int newVersion) 
     {    
      // Adding any table mods to this guy here 
     } 
    } 

    /** 
    * open the db 
    * @return this 
    * @throws SQLException 
    * return type: DBAdapter 
    */ 
    public DBAdapter open() throws SQLException 
    { 
     this.db = this.DBHelper.getWritableDatabase(); 
     return this; 
    } 

    /** 
    * close the db 
    * return type: void 
    */ 
    public void close() 
    { 
     this.DBHelper.close(); 
    } 
} 

的將對DBAdapter類只被調用時,應用程序首次啓動其唯一的責任是創建/升級表格。對數據的所有其他訪問都是通過單個「適配器」類來完成的。我發現這很好用,並且不會產生我前面提到的版本問題。

+0

噢好吧,但在Cars,Boats和Cycles類別中,我不確定我應該聲明哪個適配器。我應該在3個類中聲明DBAdapter類嗎? – Preeyah 2012-08-08 05:34:12

+0

我真的需要幫助。我無法將表插入數據庫。我在非DBAdapter類中編碼是什麼? – Preeyah 2012-08-10 03:16:26

相關問題