我正在尝试复制位于资产上的现有数据库,以便我可以访问其静态信息,但我遇到了“没有这样的表”错误。
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();
}
有什么想法吗?
答案 0 :(得分:0)
这很容易
您忘记使用扩展名
编写数据库名称 你的代码中的:
private static String DB_NAME ="CookbookDB";// Database name
应该是
private static String DB_NAME ="CookbookDB.sqlite";// Database name
或您的确切数据库扩展名是什么。
还存在一个问题。没有升级方法。例如,如果您将来发布此应用程序并且想要为应用程序进行更新。会有问题。无法看到谁使用您的应用程序并试图更新您的新数据库。
你应该小心