我试图将资产文件夹中的数据库复制到内部应用程序内存。
数据库包含两个表。一个表是完全空的,应该允许用户将他们的数据插入到表中。另一个表预加载了提供应用程序信息的数据(我需要将预加载的数据库加载到应用程序中的原因)我是否应该有两个单独的数据库,因为当我处理预加载的数据库时,数据库正在填充到应用程序内存很好,但由于某种原因,现在我不能这样做,因为那张额外的桌子。
我放入资产中的数据库是否可能已损坏,因为我里面没有 android-metadata?我的资产文件夹中的 db 是使用 CSV 表中的数据创建的。
在这个问题之前,我折射了我的包名并在清单中更改了它们。这可能是问题之一吗?
这是代码
DBHelper.class
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DB_CREATE_SQL);
db.execSQL(DB_CREATE_SQL_FOR_GAME);
}
public void createDatabase(Context context) throws IOException
{
Log.d("Create Database","In method");
// If database does not exist, copy it from the asset
if (checkDatabase() == false)
{
try
{
// Copy the database from assets
copyDatabase(context);
Log.d("Create Database","Copying");
}
catch (IOException mIOException)
{
Log.d("Create Database","Failed");
throw new Error("ErrorCopyingDataBase");
}
}
}
public void copyDatabase(Context context) throws IOException
{
// Open your local db as the input stream
InputStream myInput = context.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[10240];
int length;
while ((length = myInput.read(buffer)) > 0)
{
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public boolean checkDatabase()
{
String myPath = DB_PATH + DB_NAME;
File f = new File(myPath);
return f.exists();
}
每当我运行应用程序时,我实际上都会收到 IO 异常,因为我实际上是在应用程序启动后立即填充资产。
日志猫
09-25 16:55:42.878: E/SQLiteDatabase(28815): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
09-25 16:55:42.878: E/SQLiteDatabase(28815): at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
09-25 16:55:42.878: E/SQLiteDatabase(28815): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:278)
09-25 16:55:42.878: E/SQLiteDatabase(28815): at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:217)
09-25 16:55:42.878: E/SQLiteDatabase(28815): at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:464)
09-25 16:55:42.878: E/SQLiteDatabase(28815): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:186)
09-25 16:55:42.878: E/SQLiteDatabase(28815): at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:178)
09-25 16:55:42.878: E/SQLiteDatabase(28815): at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
09-25 16:55:42.878: E/SQLiteDatabase(28815): at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
09-25 16:55:42.878: E/SQLiteDatabase(28815): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
09-25 16:55:42.878: E/SQLiteDatabase(28815): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:669)
我试图在使用 getWritableDatabase() 创建数据库的路径后将数据库复制到文件夹中(这意味着它将覆盖任何现有的路径)并且我收到了这个错误
09-26 11:09:46.370: E/System(2444): Uncaught exception thrown by finalizer
09-26 11:09:46.380: E/System(2444): java.io.IOException: close failed: EIO (I/O error)
09-26 11:09:46.380: E/System(2444): at libcore.io.IoUtils.close(IoUtils.java:41)
09-26 11:09:46.380: E/System(2444): at java.io.RandomAccessFile.close(RandomAccessFile.java:166)
09-26 11:09:46.380: E/System(2444): at java.io.RandomAccessFile.finalize(RandomAccessFile.java:175)
09-26 11:09:46.380: E/System(2444): at java.lang.Daemons$FinalizerDaemon.doFinalize(Daemons.java:186)
09-26 11:09:46.380: E/System(2444): at java.lang.Daemons$FinalizerDaemon.run(Daemons.java:169)
09-26 11:09:46.380: E/System(2444): at java.lang.Thread.run(Thread.java:856)
09-26 11:09:46.380: E/System(2444): Caused by: libcore.io.ErrnoException: close failed: EIO (I/O error)
09-26 11:09:46.380: E/System(2444): at libcore.io.Posix.close(Native Method)
09-26 11:09:46.380: E/System(2444): at libcore.io.BlockGuardOs.close(BlockGuardOs.java:75)
09-26 11:09:46.380: E/System(2444): at libcore.io.IoUtils.close(IoUtils.java:38)
09-26 11:09:46.380: E/System(2444): ... 5 more