2012-01-12 83 views
0

其實我打算用我的SQLite瀏覽器創建的數據庫在我的Android程序。我通過一些文章閱讀並都表示使用的路徑/data/data/com.restaurant.sesame/databases/。我有點困惑,我找不到文件夾中的文件夾? 我沒有數據文件夾,或者我應該自己創建它?填充數據庫的數據到程序的Android

我放入日誌以跟蹤程序自動終止的位置,並在打開數據庫並聲明我的數據庫「沒有食物表」後終止。我檢查,它只輸出android_metadata作爲我的數據庫中唯一的表。

我的問題是,爲什麼我的數據庫只有1個表:android_metadata 只表,但沒有食物表格?我錯過了什麼步驟?

這是我的數據庫信息2個表 表1:食品(_id,名稱,價格) 表2:android_metadata

其實我跟着這個鏈接:http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

這是我的代碼處理數據庫

package com.restaurant.sesame; 

    public class Restaurant { 
    public static final String KEY_ROWID = "_id"; 
    public static final String KEY_NAME = "name"; 
    public static final String KEY_PRICE = "price"; 

    private static final String DATABASE_NAME ="Restaurantdb"; 
    private static final String DATABASE_TABLE ="Food"; 
    private static final int DATABASE_VERSION = 1; 
    private static final String DATABASE_PATH ="/data/data/com.restaurant.sesame/databases/"; 

    private static SQLiteDatabase ourDatabase; 
    private static Context ourContext = null; 
    private DBHelper ourHelper; 

    private static class DBHelper extends SQLiteOpenHelper{ 


     /** 
     * Constructor 
     * Takes and keeps a reference of the passed context in order to access to the application assets and resources. 
     * @param context 
     */ 
     public DBHelper(Context context) { 
      super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     } 

     /** 
     * Creates a empty database on the system and rewrites it with your own database. 
     * */ 
     public void createDataBase() throws IOException{ 

      boolean dbExist = checkDataBase(); 

      if(dbExist){ 
       //do nothing - database already exist 
      }else{ 

       //By calling this method and empty database will be created into the default system path 
        //of your application so we are gonna be able to overwrite that database with our database. 
       this.getReadableDatabase(); 

       try { 

        copyDataBase(); 

       } catch (IOException e) { 

        throw new Error("Error copying database"); 

       } 
      } 

     } 

     /** 
     * Check if the database already exist to avoid re-copying the file each time you open the application. 
     * @return true if it exists, false if it doesn't 
     */ 
     private boolean checkDataBase(){ 

      SQLiteDatabase checkDB = null; 

      try{ 
       String myPath = DATABASE_PATH + DATABASE_NAME; 
       checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); 

      }catch(SQLiteException e){ 

       //database does't exist yet. 

      } 

      if(checkDB != null){ 

       checkDB.close(); 

      } 

      return checkDB != null ? true : false; 
     } 

     /** 
     * 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() throws IOException{ 

      //Open your local db as the input stream 
      InputStream myInput = ourContext.getAssets().open(DATABASE_NAME); 

      // Path to the just created empty db 
      String outFileName = DATABASE_PATH + DATABASE_NAME; 

      //Open the empty db as the output stream 
      OutputStream 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); 
      } 

      //Close the streams 
      myOutput.flush(); 
      myOutput.close(); 
      myInput.close(); 

     } 

     public void openDataBase() throws SQLException{ 

      //Open the database 
      String myPath = DATABASE_PATH + DATABASE_NAME; 
      ourDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); 

     } 

     @Override 
     public synchronized void close() { 

       if(ourDatabase != null) 
        ourDatabase.close(); 

       super.close(); 

     } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 

     } 

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

     } 

      // Add your public helper methods to access and get content from the database. 
      // You could return cursors by doing "return ourDatabase.query(....)" so it'd be easy 
      // to you to create adapters for your views. 

    } 

    public Restaurant(Context c) 
    { 
     ourContext = c; 
    } 

    public Restaurant open() throws SQLException{ 
     ourHelper = new DBHelper(ourContext); 

     try { 
      ourHelper.createDataBase(); 
     } catch (IOException ioe) { 
      throw new Error("Unable to create database"); 
     } 

     try { 
      ourHelper.openDataBase(); 
     }catch(SQLException sqle){ 
      throw sqle; 
     } 
     return this; 
    } 

    public void close() 
    { 
     ourHelper.close(); 
    } 

    public String getData() 
    { 
     Log.w("my app", "IN getData TOP!!!!"); 
     String [] columns = new String [] {KEY_ROWID,KEY_NAME,KEY_PRICE}; 
     Log.w("my app", "DDDDDDDDD"); 
     /* to test what table in my database 
     Cursor c1 = ourDatabase.rawQuery("SELECT name FROM sqlite_master WHERE type = 'table'" , null); 
     String result1=""; 
     int i =0; 
     if(c1!= null){ 
      c1.moveToFirst(); 
      result1 =result1 + c1.getString(i); 
      Log.w("my app", result1); 
      i++; 
     } 
     Log.w("my app", "AAAAAAAAAA"); 
     return result1; 
     */ 
      Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null); 
     String result =""; 
     int iRow = c.getColumnIndex(KEY_ROWID); 
     int iName = c.getColumnIndex(KEY_NAME); 
     int iPrice = c.getColumnIndex(KEY_PRICE); 
     Log.w("my app", "IN getData MIDDLE!!!!"); 
     for(c.moveToFirst();!c.isAfterLast();c.moveToNext()) 
     { 
      result = result + c.getString(iRow) +"\t" + 
           c.getString(iName) + "\t" + 
           c.getString(iPrice) + "\n"; 
     } 
     Log.w("my app", "IN getData END!!!!"); 
     return result; 

    } 
} 
+0

你的問題是不明確的,你從我們的期望是什麼? – kosa 2012-01-12 18:55:27

回答

1

如果您要提供預填充的your_app.db文件以用作數據庫(相反在做所有在啓動或只能通過用戶生成的數據)INSERT語句,你將需要包括數據庫作爲原始資源,它看起來像或多或少什麼鏈接指示你做的。我猜你的數據庫複製不能正常工作,因爲DBHelper類將創建android_metadata表,如果它不存在。

在此數據庫將是隻讀的?如果是這樣,您應該可以從應用程序的原始資源中將其打開。你甚至有可能在那裏寫下它,但我不確定這是否允許。