我已经编写了这段代码来从 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
}
}