1

我在名为 Player1 的应用程序中创建了一个名为 first.db 的数据库。我想使用相同的数据库从名为 Player2 的不同应用程序创建另一个表。我还从 Player1 向 player2 提供相同的“sharedUserId”。但是当我尝试在应用程序“Player2”中创建表时,它不会转到表的 onCreate 方法。

    public static final String KEY_ID="_id";
public static final String KEY_ClIENTID="_clientid";
public static final String KEY_LOCATION="_location";
public static final String KEY_MASTERDIRECTOR="_masterdirector";
public static final String KEY_IPADDRESS="_ipaddress";
public static final String KEY_SUBNETMASK="_subnetmask";
public static final String DATABASE_TABLE ="preptool";

private static final String DB_NAME = "first.db";
private static final int DB_VERSION = 1;

private DbHelper ourhelper;
private Context context;
private SQLiteDatabase ourdatabase; 

public dataBase(Context sharedContext){
    context = sharedContext; 
}
public class DbHelper extends SQLiteOpenHelper{

    public DbHelper(Context c) {
        super(c, DB_NAME, null, DB_VERSION); 
    }
    @Override
    public void onCreate(SQLiteDatabase db) { 
        System.out.println("in");
        try{
        db.execSQL("CREATE TABLE " + DATABASE_TABLE + " ("  +
                KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                KEY_ClIENTID + " TEXT NOT NULL, " +
                KEY_LOCATION + " TEXT NOT NULL, " +
                KEY_MASTERDIRECTOR + " TEXT NOT NULL, " +
                KEY_IPADDRESS + " TEXT NOT NULL, " +                    
                KEY_SUBNETMASK + " TEXT NOT NULL);");  
        }catch(Exception e){
        }
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS" + DATABASE_TABLE ); 
        onCreate(db);
    }
}
public dataBase open()throws SQLException{
    ourhelper = new DbHelper(context);  System.out.println(context);
    ourdatabase = ourhelper.getWritableDatabase(); 
    return this;
}
public void close(){
    ourhelper.close();
}

下面的代码是我在我的 Player2 应用程序中编写的,用于从 Player1 应用程序访问数据库:

     Context sharedContext = null;
    try {
          sharedContext =     this.createPackageContext("com.xxx.android.player1",Context.CONTEXT_INCLUDE_CODE);
          if (sharedContext == null) {
                return;
          }
    } catch (NameNotFoundException e1) {
    }

    db = new dataBase(sharedContext);
    db.open(); 
4

0 回答 0