2011-11-02 77 views
0

我正在製作一個android應用程序,我需要在android中的相同數據庫中創建不同的表格我是否必須爲此創建不同的數據庫輔助類? 謝謝Android:數據庫

回答

4

你已經有一個SQLite數據庫,你要複製的資產數據庫,以便代碼是在這裏:

public class DataBaseHelper extends SQLiteOpenHelper{ 
private Context mycontext; 

private String DB_PATH = "/data/data/com.example.android.podemo/databases/"; 
private static String DB_NAME = "UrClassname.sqlite"; 
public SQLiteDatabase myDataBase; 


public DataBaseHelper(Context context) throws IOException { 
    super(context,DB_NAME,null,1); 
    this.mycontext=context; 
    boolean dbexist = checkdatabase(); 
    if(dbexist) 
    { 
     System.out.println("Database exists"); 
     opendatabase(); 
    } 
    else 
    { 
     System.out.println("Database doesn't exist"); 
    createdatabase(); 
    } 

} 

public void createdatabase() throws IOException{ 
    boolean dbexist = checkdatabase(); 
    if(dbexist) 
    { 
     System.out.println(" Database exists."); 
    } 
    else{ 
     this.getReadableDatabase(); 
    try{ 
      copydatabase(); 
     } 
     catch(IOException e){ 
      throw new Error("Error copying database"); 
     } 
    } 
} 
private boolean checkdatabase() { 
    //SQLiteDatabase checkdb = null; 
    boolean checkdb = false; 
    try{ 
     String myPath = DB_PATH + DB_NAME; 
     File dbfile = new File(myPath); 
     //checkdb = SQLiteDatabase.openDatabase(myPath,null,SQLiteDatabase.OPEN_READWRITE); 
     checkdb = dbfile.exists(); 
    } 
    catch(SQLiteException e){ 
     System.out.println("Database doesn't exist"); 
    } 

    /*if(checkdb!=null){ 
     checkdb.close(); 
    } 
    return checkdb!=null? true :false;*/ 
    return checkdb; 
} 
private void copydatabase() throws IOException { 

    //Open your local db as the input stream 
    InputStream myinput = mycontext.getAssets().open(DB_NAME); 

    // Path to the just created empty db 
    String outfilename = DB_PATH + DB_NAME; 

    //Open the empty db as the output stream 
    OutputStream myoutput = new FileOutputStream("/data/data/com.example.android.podemo/databases/PartyDetails.sqlite"); 

    // transfer byte to inputfile to outputfile 
    byte[] buffer = new byte[1024]; 
    int length; 
    while ((length = myinput.read(buffer))>0) 
    { 
     myoutput.write(buffer,0,length); 
    } 

    //Close the streams 
    myoutput.flush(); 
    myoutput.close(); 
    myinput.close(); 

} 

public void opendatabase() throws SQLException 
{ 
    //Open the database 
    String mypath = DB_PATH + DB_NAME; 
    myDataBase = SQLiteDatabase.openDatabase(mypath, null, SQLiteDatabase.OPEN_READWRITE); 

} 



public synchronized void close(){ 
    if(myDataBase != null){ 
     myDataBase.close(); 
    } 
    super.close(); 
} 

@Override 
public void onCreate(SQLiteDatabase db) { 
} 

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

您需要在同一個數據庫中只製作不同的表格。請參閱Sqlite tutorial以獲得更多的清晰度。

+0

該教程這是給在輔助類create table命令只 – ekjyot

+0

你可以執行命令無論你希望在db實例的幫助下,你總是可以根據你的需求改變做事的方式...... –