2013-07-02 59 views
0

我已經寫了這段代碼從SD卡訪問數據庫...但我得到一個錯誤沒有找到這樣的表..plz幫助我解決這個問題。沒有這樣的表發現

我的代碼:

package sai.electricals.photo.meter.verification.system; 

import java.io.File; 
import java.io.FileInputStream; 
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.SQLiteOpenHelper; 
import android.os.Environment; 
import android.util.Log; 


class DataBaseHelper extends SQLiteOpenHelper { 
private static String DB_NAME ="master.sqlite";// Database name 
private static String TAG = "DataBaseHelper"; // Tag just for the LogCat window 
private static String DB_PATH = "/sdcard/master.sqlite"; 
private SQLiteDatabase mDataBase; 
private final Context mContext; 

DataBaseHelper(final Context context) { 
    super(context, Environment.getExternalStorageDirectory() 
      + File.separator + "/DataBase/" + File.separator 
      + DB_NAME, null, 1); 
    this.mContext = context; 
    Log.e("ERROR HERE", "1"); 
} 




public void createDataBase() throws IOException 
{ 
//If database not exists copy it from the assets 

boolean mDataBaseExist = checkDataBase(); 
if(!mDataBaseExist) 
{ 
    this.getReadableDatabase(); 
    this.close(); 
    // try 
    //{ 
     //Copy the database from assests 
     copyDataBase(); 
     Log.e(TAG, "createDatabase database created"); 
    // } 
    //catch (IOException mIOException) 
    //{ 
     // throw new Error("ErrorCopyingDataBase"); 
    //} 
} 
} 
//Check that the database exists here: mnt/sdcard 
private boolean checkDataBase() 
{ 
    File dbFile = new File(DB_PATH); 
    Log.v("dbFile", dbFile + " "+ dbFile.exists()); 
    return dbFile.exists(); 
} 

//Copy the database from SDCARD 
private void copyDataBase() throws IOException 
{ 
    InputStream mInput; 
    mInput = new FileInputStream(DB_NAME); 
    File directory = new File(DB_PATH); 
    // String outFileName = DB_PATH; 
    if (!directory.exists()) { 
     directory.mkdirs(); 
    } 
    OutputStream mOutput = new FileOutputStream(directory 
      .getPath()); 
    byte[] mBuffer = new byte[1024]; 
    int mLength; 
    while ((mLength = mInput.read(mBuffer))>0) 
    { 
     mOutput.write(mBuffer, 0, mLength); 
    } 
    mOutput.flush(); 
    mOutput.close(); 
    mInput.close(); 
    Log.e("HELLO", "I M IN COPYDATABSE"); 
} 

//Open the database, so we can query it 
public boolean openDataBase() throws SQLException 
{ 
    String mPath = DB_PATH; 
    Log.v("mPath", mPath); 
    mDataBase = SQLiteDatabase.openOrCreateDatabase(mPath,null); 
    //mDataBase = SQLiteDatabase.openDatabase(mPath, null,   SQLiteDatabase.NO_LOCALIZED_COLLATORS); 
    return mDataBase != null; 
} 

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

@Override 
public void onCreate(SQLiteDatabase arg) { 
    //arg.execSQL(createCredentias); 
    //Log.e("secondtable", "credentials created"); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    // TODO Auto-generated method stub 

} 



} 
+0

你有沒有檢查你的數據庫是否被創建? – Riser

+0

@Segi是的,checkDatabase方法返回true。 –

+0

嘗試像Joel下面說的那樣... – Riser

回答

0

Environment.getExternalStorageDirectory() + File.separator + "/DataBase/" + File.separator + DB_NAME/sdcard/master.sqlite並不等同,會有所不同。此外getExernalStorageDirectory()將在沒有外部存儲器的設備上返回null(請參閱HTC One,非常流行)。如果您已經在使用它,我不明白爲什麼要添加分隔符。

此外,我不確定您是否可以在其他地方使用數據庫,而不是/data/data/{packagename}/databases。更不用說它是否是一個好設計。

我建議放入shell並檢查是否有上述目錄中創建的文件。

編輯:如果你想使用外部數據庫,只需將它複製到apps文件夾中的database目錄。然後通過將文件名傳遞給DatabaseHelper的構造函數來打開它。

+0

PLZ送我糾正的代碼,因爲我是一個新手在Android開發 –

0

改變你的構造類似這樣的,

public DataBaseHelper (Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    } 

,並宣佈這些下面的班開班;

private static final int DATABASE_VERSION = 1; 
private static final String DATABASE_NAME = "yourDatabaseName";