2013-05-27 34 views
2

我是新來的android和我必須建立一個Android應用程序,這將是一個測驗,在這我必須從一個給定的表每次挑選隨機問題。我已經建立了數據庫可以任何人幫助我如何導入到我的應用程序。 我打算使用一個意圖,每次都會調用同一個活動,並且每次在活動中都會隨機選擇一個。生成它會檢查它是否生成較早或沒有,如果不是,那麼它會從數據庫中挑選問題。如何導入已建成的數據庫到Android應用程序

任何其他建議來實現從數據庫檢索問題或實現這個應用程序也是受歡迎的。

+1

難道你不能只使用文件複製從你的資產中複製數據庫到應用程序的數據位置? – rwilliams

回答

1

正如@rwilliams說你剛剛從資產數據庫複製到App目錄..ü只需要檢查的天氣ü已經複製它或不..

這裏就是我如何做到這一點。

只需將您的數據庫置於資產目錄中即可。

public class DatabaseHelper extends SQLiteOpenHelper { 

    public static String DB_PATH = "/data/data/com.aavid.advance.alarm.clock/databases/"; 
    private String dbName = "world_time.db"; 
    protected SQLiteDatabase theDatabase; 
    private final Context context; 

    public DatabaseHelper(Context c, String dbName){ 
     super(c,dbName, null, 1); 
     this.dbName = dbName; 
     this.context = c; 
    } 

    public void createDataBase() throws IOException{ 

     boolean dbExist = checkDataBase(); 

     if(dbExist){ 
      //do nothing - database already exist 
     }else{ 
      this.getReadableDatabase(); 
      try { 
       copyDataBase(); 
      } catch (IOException e) { 
       throw new Error("Error copying database"); 
      } 
     } 
    } 

    private boolean checkDataBase(){ 
     SQLiteDatabase checkDB = null; 
     try{ 
      String path = DB_PATH + dbName; 
      checkDB = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READONLY); 
     }catch(SQLiteException e){ 
     } 
     if(checkDB != null){ 
      checkDB.close(); 
     } 
     return checkDB != null ? true : false; 
    } 

    private void copyDataBase() throws IOException{ 

     File f = new File(DB_PATH); 
     f.mkdirs(); 

     InputStream myInput = context.getAssets().open(dbName); 
     String outFileName = DB_PATH + dbName; 
     OutputStream myOutput = new FileOutputStream(outFileName); 
     byte[] buffer = new byte[1024]; 
     int length; 
     while ((length = myInput.read(buffer))>0){ 
      myOutput.write(buffer, 0, length); 
     } 
     myOutput.flush(); 
     myOutput.close(); 
     myInput.close(); 
    } 

    public void openDataBase() throws SQLException{ 
     String myPath = DB_PATH + dbName; 
     theDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY|SQLiteDatabase.NO_LOCALIZED_COLLATORS); 
    } 

    @Override 
    public synchronized void close() { 
     if(theDatabase != null) 
      theDatabase.close(); 
     super.close(); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 

    } 

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

    } 

} 
+1

+1爲完整的示例 – rwilliams

0

什麼納賽爾回答是正確的,但讓我增加一些註釋:

如果你改變你的DB(添加字段,更改數據類型,...),

千萬記住更改您的數據庫文件名稱。

如果你沒有這樣做,你的APP不會讀取最新的DB文件,除非你卸載APP並重新安裝。

0

用戶可以根據需要清除數據。所以你不能讓你的數據庫變成靜態的。 但是,如果要保留數據庫,可以將數據庫複製到資產文件夾,並在應用程序調用SQLiteHelper的方法onCreate時,將從資產文件夾複製到設備。

Def.DBNAME = "youdb.sqlite"; 

下面是示例:

DBHelper

package vn.mve.db; 

import java.util.List; 

public interface DBHelper<T> { 
    boolean insert(T val); 
    boolean update(T val); 
    boolean delete(T val); 
    List<T> getList(int type); 
    T getChild(Object val); 
} 

DBHandler

public class DBHandler extends SQLiteOpenHelper { 
    private static final String TAG = DBHandler.class.getSimpleName(); 
    protected SQLiteDatabase db; 
    private final Context context; 
    private static String PACKAGE_NAME = ""; 
    private static int DATABASE_VERSION = 1; 

    public DBHandler(Context context) { 
     super(context, Def.DBNAME, null, DATABASE_VERSION); 
     this.context = context; 
     PACKAGE_NAME = this.context.getPackageName(); 
     Def.FOLDER_DB = "/data/data/" + PACKAGE_NAME + "/databases/"; 
     Log.d(TAG, Def.FOLDER_DB); 
     try { 
      this.createDataBase(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     }  
    } 
    @Override 
    public void onCreate(SQLiteDatabase db) { 
     try { 
      this.createDataBase(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     context.deleteDatabase(Def.DBNAME); 
     onCreate(db); 
    } 

    public void createDataBase() throws IOException{ 
     // for first database; 
     boolean dbExist = checkDataBase(); 
     if(!dbExist){ 
      try { 
       copyDataBase("db/" + Def.DBNAME); 
      } catch (Exception e) { 
       Log.e(TAG, "createDatabse -> Copy failed!"); 
       throw new Error("Error copying database"); 
      } 
     } else { 
      open(); 
      boolean isExist = false; 
      Cursor cursor = db.rawQuery("select DISTINCT tbl_name from sqlite_master where tbl_name = 'config'", null); 
      if (cursor != null) { 
       isExist = true; 
       cursor.close(); 
      } else { 
       isExist = false; 
      } 
      close(); 
      Log.d(TAG, isExist + ""); 
      if (!isExist) { 
       this.context.deleteDatabase(Def.DBNAME); 
       try { 
        Log.d(TAG, "createDatabase when database has existed"); 
        copyDataBase(Def.DBNAME); 
       } catch (Exception e) { 
        Log.e(TAG, "createDatabse -> Copy failed!"); 
        throw new Error("Error copying database"); 
       }    
      } 
     } 
    } 
    /** 
    * Copies your database from your local assets-folder to the just created empty database in the 
    * system folder, from where it can be accessed and handled. 
    * This is done by transfering bytestream. 
    * */ 
    private void copyDataBase(String DB) { 
     //Open your local db as the input stream 
     InputStream myInput = null; 
     //Open the empty db as the output stream 
     OutputStream myOutput = null; 
     try { 
      myInput = context.getResources().getAssets().open(DB); 

      // Path to the just created empty db 
      String outFileName = Def.FOLDER_DB + Def.DBNAME; 
      myOutput = new FileOutputStream(outFileName); 

      //transfer bytes from the inputfile to the outputfile 
      byte[] buffer = new byte[1024]; 
      int length; 
      while ((length = myInput.read(buffer)) > 0){ 
       myOutput.write(buffer, 0, length); 
      } 
     } catch (FileNotFoundException e) { 
      Log.e(TAG, "copyDatabase -> File not found."); 
      e.printStackTrace(); 
     } catch (IOException e) { 
      Log.e(TAG, "copyDatabase"); 
     } finally { 
       //Close the streams 
      try { 
       myOutput.flush(); 
       myOutput.close(); 
       myInput.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    } 
    private boolean checkDataBase(){ 
     boolean checkDB = false; 
     try{ 
      String myPath = Def.FOLDER_DB + Def.DBNAME; 
      File dbFile = new File(myPath); 
      checkDB = dbFile.isFile(); 
      Log.d(TAG, "checkDatabase: " + String.valueOf(checkDB)); 
      try { 
       File fTmp = new File(Def.FOLDER_DB); 
       if (!fTmp.exists()) { 
        fTmp.mkdir(); 
       } 
      } catch (Exception e) { 
       Log.e(TAG, "checkDatabase" + e.getMessage()); 
      } 
     }catch(SQLiteException e){} 
     return checkDB; 
    } 
    public void open() { 
     try { 
      String myPath = Def.FOLDER_DB + Def.DBNAME; 
      db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);   
     } catch (Exception e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

    @Override 
    public synchronized void close() { 
      if(db != null) 
       db.close(); 
      super.close(); 
    }   
    public SQLiteDatabase getSqlDb() { 
     return db; 
    } 
    public void setSqlDb(SQLiteDatabase sqlDb) { 
     this.db = sqlDb; 
    }  
} 

在這裏:

public class MVideo extends DBHandler implements DBHelper<Video> { 
    public static final String TAG = MVideo.class.getSimpleName(); 
    public MVideo(Context context) { 
     super(context); 
    } 

    @Override 
    public boolean insert(Video val) { 
     open(); 
     ContentValues cValues = new ContentValues(); 
     cValues.put(Def.Video.ID, val.getId()); 
     cValues.put(Def.Video.TITLE, val.getTitle()); 
     cValues.put(Def.Video.THUMBNAIL, val.getThumbnail()); 
     cValues.put(Def.Video.DESCRIPTION, val.getDescription()); 
     cValues.put(Def.Video.ENGLISH, val.getEnglish()); 
     cValues.put(Def.Video.VIETNAMESE, val.getVietnamese()); 
     cValues.put(Def.Video.ISVIEW, val.getIsView()); 
     long result = db.insert(Def.Video.NAME, null, cValues); 
     close();   
     return result > 0; 
    } 

    public boolean insertList(List<Video> list) { 
     open(); 
     db.execSQL("BEGIN IMMEDIATE TRANSACTION"); 
     for (Video v : list) { 
      db.execSQL(String.format("INSERT INTO " + Def.Video.NAME + " (\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\") VALUES" + 
        " (\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\")", 
        Def.Video.ID, Def.Video.TITLE, Def.Video.THUMBNAIL, Def.Video.DESCRIPTION, 
        Def.Video.ENGLISH, Def.Video.VIETNAMESE, Def.Video.ISVIEW, 
        v.getId(), v.getTitle(), v.getThumbnail(), v.getDescription(), v.getEnglish(), v.getVietnamese(), v.getIsView() + "")); 
      Log.d(TAG, "insertList -> " + v.toString()); 
     } 
     db.execSQL("COMMIT TRANSACTION"); 
     close();   
     return true; 
    } 

    @Override 
    public boolean update(Video val) { 
     // TODO Auto-generated method stub 
     return false; 
    } 

    @Override 
    public boolean delete(Video val) { 
     open(); 
     db.delete(Def.Video.NAME, Def.Video.ID + "=?", new String[]{val.getId()}); 
     close(); 
     return false; 
    } 

    @Override 
    public List<Video> getList(int type) { 
     List<Video> list = new ArrayList<Video>(); 
     open(); 
     Cursor c = db.rawQuery(Def.Video.GET_ALL, null); 
     if (c.moveToFirst()) { 
      while (c.moveToNext()) { 
       String ID = c.getString(0); 
       String title = c.getString(1); 
       String thumbnail = c.getString(2); 
       String description = c.getString(3); 
       String english = c.getString(4); 
       String vietnamese = c.getString(5); 
       boolean isView = Boolean.parseBoolean(c.getString(6)); 
       list.add(new Video(ID, title, thumbnail, description, english, vietnamese, isView)); 
      } 
     } 
     close(); 
     return list; 
    } 

    @Override 
    public Video getChild(Object val) { 
     open(); 
     Cursor c = db.query(Def.Video.NAME, new String[]{ 
       Def.Video.ID, Def.Video.TITLE, Def.Video.THUMBNAIL, Def.Video.DESCRIPTION, 
       Def.Video.ENGLISH, Def.Video.VIETNAMESE, Def.Video.ISVIEW 
     }, Def.Video.ID + "=?", new String[]{val.toString()}, null, null, null, null); 
     if (c != null) { 
      c.moveToFirst(); 
     } 
     Video v = new Video(c.getString(0), c.getString(1), 
       c.getString(2), c.getString(3), c.getString(4), 
       c.getString(5), Boolean.parseBoolean(c.getString(6))); 
     close(); 
     return v; 
    } 
} 
相關問題