我正在尝试复制位于资产上的现有数据库,以便我可以访问其静态信息,但我遇到了“没有这样的表”错误。
Logcat(其中一些,如果需要我会发布更多):
11-25 01:30:49.863: E/SQLiteLog(627): (1) no such table: myTable
11-25 01:30:49.873: E/DataAdapter(627): getTestData >>android.database.sqlite.SQLiteException: no such table: myTable (code 1): , while compiling: SELECT * FROM myTable
11-25 01:30:49.873: D/AndroidRuntime(627): Shutting down VM
11-25 01:30:49.873: W/dalvikvm(627): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
11-25 01:30:49.882: E/AndroidRuntime(627): FATAL EXCEPTION: main
11-25 01:30:49.882: E/AndroidRuntime(627): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.adiel.cookbook/com.adiel.cookbook.MainActivity}: android.database.sqlite.SQLiteException: no such table: myTable (code 1): , while compiling: SELECT * FROM myTable
11-25 01:30:49.882: E/AndroidRuntime(627): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
11-25 01:30:49.882: E/AndroidRuntime(627): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
数据库处理程序:
import java.io.File;
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.util.Log;
public class DBHandler extends SQLiteOpenHelper
{
private static String TAG = "DataBaseHelper"; // Tag just for the LogCat window
//destination path (location) of our database on device
private static String DB_PATH = "";
private static String DB_NAME ="CookbookDB";// Database name
private SQLiteDatabase mDataBase;
private final Context mContext;
public DBHandler(Context context)
{
super(context, DB_NAME, null, 1);// 1? its Database Version
DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
this.mContext = context;
}
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 assets
copyDataBase();
Log.e(TAG, "createDatabase database created");
}
catch (IOException mIOException)
{
throw new Error("ErrorCopyingDataBase");
}
}
}
// Check that the database exists here: /data/data/your package/databases/DaName
private boolean checkDataBase()
{
File dbFile = new File(DB_PATH + DB_NAME);
//Log.v("dbFile", dbFile + " "+ dbFile.exists());
return dbFile.exists();
}
//Copy the database from assets
private void copyDataBase() throws IOException
{
InputStream mInput = mContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream mOutput = new FileOutputStream(outFileName);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer))>0)
{
mOutput.write(mBuffer, 0, mLength);
}
mOutput.flush();
mOutput.close();
mInput.close();
}
有任何想法吗?