2013-01-03 30 views
7

我覺得Android開發人員關於在SQLite中保存數據的指導方針真的很缺乏。當我涉及到多個表格時,我對這個通用指南是什麼感到困惑。關於SQLiteOpenHelper和創建多個表的困惑

我目前有兩個經理向用戶界面公開兩個不同的實體集合(CRUD)。

我是否:

創建每個經理中的私有類SQLiteOpenHelper。每個助手都有自己的onCreate用於自己的TABLE?

創建一個單一的公共類SQLiteOpenHelper創建兩個表的?

我沒有看到任何明顯的優勢,使用上面的一個,但我看到他們兩個正在使用。 Android對此有何評論?

+1

您必須遵循此選項。創建一個創建TABLE的公共類SQLiteOpenHelper? – itsrajesh4uguys

回答

7

請使用下面的代碼來創建多個表(我現在已經創建了兩個表)。我已經完成了創建和插入的代碼。也..

import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.util.Log; 

public class MyDB { 
    public static final String KEY_ROWID = "_id"; 
     public static final String KEY_FIRSTNAME = "ID"; 
     public static final String KEY_LASTNAME = "CS"; 
     public static final String KEY_DESIGNATION = "CN"; 
     public static final String KEY_DN = "DN"; 

     private static final String TAG = "MyDB";  
     private static final String DATABASE_NAME = "test.db";  
     private static final int DATABASE_VERSION = 1; 

     private static final String DATABASE_CREATE_ValidateUser_DriverInfo = 
      "create table tabletest1 (_id integer primary key autoincrement, " 
      + "ID text not null, CS text not null,CN text not null,DN text not null);"; 

     private static final String DATABASE_CREATE_ValidateUser_TripInfo = 
       "create table tabletest2 (_id integer primary key autoincrement, " 
       + "TI text not null, PU text not null,LN text not null,FN text not null,Origin varchar not null,De text not null);"; 


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

     public MyDB(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_ValidateUser_DriverInfo); 
       db.execSQL(DATABASE_CREATE_ValidateUser_TripInfo); 
      } 

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


     public MyDB open() throws SQLException 
     { 
      db = DBHelper.getWritableDatabase(); 
      return this; 
     } 

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


     public long insertTitle(ContentValues initialValues,String TableName) 
     { 


      return db.insert(TableName, null, initialValues); 
     } 




} 

使用下面的代碼從您所需的活動插入數據。

MyDB mmdb=new MyDB(getBaseContext()); 
       mmdb.open(); 

initialValues = new ContentValues(); 
          initialValues.put("ID", ID); 
          initialValues.put("CS", CS); 
          initialValues.put("CN", CN); 
          initialValues.put("DN", DN); 



         mmdb.insertTitle(initialValues,"tabletest1"); 

mmdb.close(); 
+0

是否建議在同一個數據庫而不是多個數據庫中創建多個表,即使這些表之間沒有連接? –