gpt4 book ai didi

android - 从 Assets 文件夹问题复制 SQLite 数据库

转载 作者:太空狗 更新时间:2023-10-29 12:45:43 25 4
gpt4 key购买 nike

有人能指出我正确的方向吗?我可以在/data/data/mypacket/databases 中创建一个空数据库,但我无法从 Assets 文件夹中复制我的数据库?有什么建议么?感谢您的帮助

代码:

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 DataBaseHelper 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 ="MyDatabase";// Database name
private SQLiteDatabase mDataBase;
private final Context mContext;

public DataBaseHelper(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 assests
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/Da Name
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();
}

//Open the database, so we can query it
public boolean openDataBase() throws SQLException
{
String mPath = DB_PATH + DB_NAME;
//Log.v("mPath", mPath);
mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.CREATE_IF_NECESSARY);
//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 arg0) {
// TODO Auto-generated method stub

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub

}

}

洛卡特:

 09-03 02:22:32.459: E/AndroidRuntime(19099): FATAL EXCEPTION: main
09-03 02:22:32.459: E/AndroidRuntime(19099): java.lang.IllegalStateException: Could not execute method of the activity
09-03 02:22:32.459: E/AndroidRuntime(19099): at android.view.View$1.onClick(View.java:3597)
09-03 02:22:32.459: E/AndroidRuntime(19099): at android.view.View.performClick(View.java:4202)
09-03 02:22:32.459: E/AndroidRuntime(19099): at android.view.View$PerformClick.run(View.java:17340)
09-03 02:22:32.459: E/AndroidRuntime(19099): at android.os.Handler.handleCallback(Handler.java:725)
09-03 02:22:32.459: E/AndroidRuntime(19099): at android.os.Handler.dispatchMessage(Handler.java:92)
09-03 02:22:32.459: E/AndroidRuntime(19099): at android.os.Looper.loop(Looper.java:137)
09-03 02:22:32.459: E/AndroidRuntime(19099): at android.app.ActivityThread.main(ActivityThread.java:5039)
09-03 02:22:32.459: E/AndroidRuntime(19099): at java.lang.reflect.Method.invokeNative(Native Method)
09-03 02:22:32.459: E/AndroidRuntime(19099): at java.lang.reflect.Method.invoke(Method.java:511)
09-03 02:22:32.459: E/AndroidRuntime(19099): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-03 02:22:32.459: E/AndroidRuntime(19099): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-03 02:22:32.459: E/AndroidRuntime(19099): at dalvik.system.NativeStart.main(Native Method)
09-03 02:22:32.459: E/AndroidRuntime(19099): Caused by: java.lang.reflect.InvocationTargetException
09-03 02:22:32.459: E/AndroidRuntime(19099): at java.lang.reflect.Method.invokeNative(Native Method)
09-03 02:22:32.459: E/AndroidRuntime(19099): at java.lang.reflect.Method.invoke(Method.java:511)
09-03 02:22:32.459: E/AndroidRuntime(19099): at android.view.View$1.onClick(View.java:3592)
09-03 02:22:32.459: E/AndroidRuntime(19099): ... 11 more
09-03 02:22:32.459: E/AndroidRuntime(19099): Caused by: java.lang.Error: Error copying database
09-03 02:22:32.459: E/AndroidRuntime(19099): at com.petermihaylov.scjajavaexamquestions.DataBaseHelper.createDataBase(DataBaseHelper.java:56)
09-03 02:22:32.459: E/AndroidRuntime(19099): at com.petermihaylov.scjajavaexamquestions.TestAdapter.createDatabase(TestAdapter.java:25)
09-03 02:22:32.459: E/AndroidRuntime(19099): at com.petermihaylov.scjajavaexamquestions.TrueAndFalse.testasd(TrueAndFalse.java:25)
09-03 02:22:32.459: E/AndroidRuntime(19099): ... 14 more

最佳答案

一些提示:

  • 检查文件 `DB_NAME` 是否存在于应用程序的“assets”文件夹中(而不是“raw”文件夹中)。
  • 如果 Assets 文件扩展名为“.gz”,则该扩展名将被删除。
  • Assets 文件不应大于 1MB。如果是这种情况,请尝试将其压缩并解压到数据库文件夹中。
  • 在复制文件之前检查 `outFileName` 的路径是否存在(即 `new File(outFileName).getParentFile().mkdirs();`
  • 根据 API 版本检查 `DB_PATH` 是否正确构建。

this answer 中所述:

if(android.os.Build.VERSION.SDK_INT >= 4.2){
DB_PATH = context.getApplicationInfo().dataDir + "/databases/";
}
else
{
DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
}

此外,不要忘记创建 android_metadata 表,如 this answer 中所述。 .

关于android - 从 Assets 文件夹问题复制 SQLite 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18584032/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com