2012-10-01 112 views
0

我爲我的項目創建了自己的數據庫,但是我在推送數據/數據文件夾時遇到了麻煩。
我首先在我的手機上測試過它,併成功推送了我創建的數據庫。
現在我正面臨着在另一部手機上測試它的問題。我得到一個錯誤使用自己的數據庫

android.database.sqlite.SQLiteException: no such table: tblFirstAid: , while compiling: SELECT faName FROM tblFirstAid 

我把我的數據庫中的資產文件夾,這是我根深蒂固的手機上運行良好。
我會添加什麼代碼來將它從資產文件夾複製到/ data/data目錄中。
我已經嘗試了一個reigndesign,我得到了這個錯誤。

這裏是我的DB助手類

package com.dr.droid.lee; 

import java.io.IOException; 
import java.util.ArrayList; 

import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteOpenHelper; 
import android.net.http.SslCertificate.DName; 
import android.util.Log; 

public class DbHelper { 

    public static final String Row_id = "_id"; 
    public static final String Row_Name = "faName"; 
    public static final String Row_Desc = "faInfo"; 
    private static final String db_Name = "dbDrDroid"; 
    private static final String db_Table = "tblFirstAid"; 
    private static final String db_HTable = "tblHospitals"; 
    private static final String Row_HosName = "HospitalName"; 
    private static final String Row_HosAdd = "Address"; 
    private static final String Row_HosReg = "Region"; 
    private static final String Row_HosCity = "City"; 
    private static final String Row_HosContact = "Contact"; 
    private static final String Row_dName = "dName"; 
    private static final String Row_dTable = "tblDisease"; 
    private static final int db_Version = 1; 
    private dbhelp ourhelper; 
    private static Context ourcontext; 
    private SQLiteDatabase ourDB; 

    private static class dbhelp extends SQLiteOpenHelper{ 

     public dbhelp(Context context) { 
      super(context, db_Name, null, db_Version); 
      // TODO Auto-generated constructor stub 
     } 

     @Override 
     public void onCreate(SQLiteDatabase db) { 
      // TODO Auto-generated method stub 



      db.execSQL("CREATE TABLE IF NOT EXISTS " + db_Table + " (" + Row_id + " INTEGER PRIMARY KEY AUTOINCREMENT, " 
       + Row_Name + " TEXT NOT NULL, " 
       + Row_Desc + " TEXT NOT NULL)"); 

     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      // TODO Auto-generated method stub 
      db.execSQL("DROP TABLE IF EXISTS" + db_Table); 
      onCreate(db); 

     } 

    } 

    public DbHelper (Context c){ 
     ourcontext= c; 
    } 

    public DbHelper open(){ 
     ourhelper = new dbhelp(ourcontext); 
     ourDB = ourhelper.getWritableDatabase(); 
     return this; 
    } 

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

    public ArrayList<String> getFAData() { 
     // TODO Auto-generated method stub 
     ArrayList<String> comments = new ArrayList<String>(); 
     String [] columns = new String[]{Row_Name}; 
     Cursor c = ourDB.query(db_Table, columns, null, null, null, null, null); 
     int iRow = c.getColumnIndex(Row_Name); 
     c.moveToFirst(); 
     while (!c.isAfterLast()) {   
      comments.add(c.getString(iRow)); 
      c.moveToNext(); 
     } 

     c.close(); 
     return comments; 
    } 

    public ArrayList<String> getHData(){ 
     ArrayList<String> res = new ArrayList<String>(); 
     String [] columns = new String []{Row_HosName}; 
     Cursor h = ourDB.query(db_HTable, columns, null, null, null, null, Row_HosName); 
     int HRow = h.getColumnIndex(Row_HosName); 
     h.moveToFirst(); 
     while (!h.isAfterLast()) {   
       res.add(h.getString(HRow)); 
       h.moveToNext(); 
      } 

      h.close(); 
      return res; 

    } 

    public ArrayList<String> getDData(){ 
     ArrayList<String> res = new ArrayList<String>(); 
     String [] columns = new String []{Row_dName}; 
     Cursor d = ourDB.query(Row_dTable, columns, null, null, null, null, null); 
     int HRow = d.getColumnIndex(Row_dName); 
     d.moveToFirst(); 
     while (!d.isAfterLast()) {   
       res.add(d.getString(HRow)); 
       d.moveToNext(); 
      } 

      d.close(); 
      return res; 

    } 



} 

這裏是我從reigndesign

package com.dr.droid.lee; 

import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 

import android.content.Context; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteException; 
import android.database.sqlite.SQLiteOpenHelper; 

public class DataBaseHelper extends SQLiteOpenHelper{ 

    //The Android's default system path of your application database. 
    private static String DB_PATH = "/data/data/com.dr.droid.lee/databases/"; 

    private static String DB_NAME = "dbDrDroid"; 

    private SQLiteDatabase myDataBase; 

    private final Context myContext; 

    /** 
    * Constructor 
    * Takes and keeps a reference of the passed context in order to access to the application assets and resources. 
    * @param context 
    */ 
    public DataBaseHelper(Context context) { 

     super(context, DB_NAME, null, 1); 
     this.myContext = context; 
    } 

    /** 
    * 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 { 
       this.close(); 
       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 = DB_PATH + DB_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 = 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(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 = DB_PATH + DB_NAME; 
     myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); 

    } 

    @Override 
    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) { 

    } 
} 

有些我是多麼仍然得到錯誤,如果我的我不把我的數據庫所使用的類我的手機。

+0

http://stackoverflow.com/questions/9109438/how-to-use-existing-database-with-android-app/9109728#9109728 –

+0

示例項目在這裏http://sdrv.ms/N857Wn –

回答

0

我假設你的DB_NAME與存在於你的assests文件夾中的數據庫不一樣。

如果你正在跟蹤 「reigndesign」,然後檢查這一行:這裏

private static String DB_NAME ="myDBName"; 

DB_NAME是數據庫的名稱。假定您在資產文件夾中有數據庫的副本。您的DB_NAME必須與資產文件夾中存在的相同。

跟隨this

+0

我先生已經檢查過它。它與我的資產文件夾中的一樣 – leenolasco

+0

嘗試此示例項目:http://sdrv.ms/N857Wn –

+0

我現在工作了,先生,謝謝您發佈的鏈接 – leenolasco

0

當你給它一個不存在的文件名時,SQLite會高興地創建一個新的空數據庫。

確保用於複製和打開數據庫的文件名相同。

+0

因此生病的是調用複製方法ryt在oncreate數據庫方法之後? – leenolasco

+0

是的。什麼是用於複製和打開的文件名? –

+0

dbDrDroid是我正在使用的數據庫的文件名,我編輯了我的帖子並粘貼了我的代碼。 – leenolasco

相關問題