0

我已经编写了这段代码来从 sdcard 访问数据库......但是我收到了 No such table found 的错误......请帮我解决这个问题。

我的代码:

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

} 



 }
4

2 回答 2

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";
于 2013-07-02T10:43:17.000 回答
0

Environment.getExternalStorageDirectory() + File.separator + "/DataBase/" + File.separator + DB_NAME并且/sdcard/master.sqlite不等效并且会有所不同。此外getExernalStorageDirectory(),将在没有外部存储器的设备上返回 null(参见 HTC One,非常受欢迎)。如果您已经在使用它,我不明白为什么要添加分隔符。

此外,我不确定您是否真的可以在/data/data/{packagename}/databases. 更不用说它是否是一个好的设计。

我建议进入 shell 并检查是否在上述目录中创建了文件。

编辑:如果您想使用外部数据库,只需将其复制到database应用程序文件夹中的目录中。然后通过将文件名传递给 DatabaseHelper 的构造函数来打开它。

于 2013-07-02T09:17:27.003 回答