gpt4 book ai didi

android - 从 Assets 文件夹错误读取sqlite

转载 作者:太空宇宙 更新时间:2023-11-03 10:40:00 25 4
gpt4 key购买 nike

我的Databasehelper.class

public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DBNAME = "db3000.sqlite";
public static final String DBLOCATION = "/data/data/com.gnirt69.sqlitefromassetexample/databases/";
private Context mContext;
private SQLiteDatabase mDatabase;

public DatabaseHelper(Context context) {
super(context, DBNAME, null, 1);
this.mContext = context;
}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

public void openDatabase() {
String dbPath = mContext.getDatabasePath(DBNAME).getPath();
if(mDatabase != null && mDatabase.isOpen()) {
return;
}
mDatabase = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);
}

public void closeDatabase() {
if(mDatabase!=null) {
mDatabase.close();
}
}
public ArrayList<word> getListWord() {
word product = null;
ArrayList<word> productList = new ArrayList<>();
openDatabase();
Cursor cursor = mDatabase.rawQuery("SELECT * FROM word", null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
product = new word(cursor.getInt(0), cursor.getString(1), cursor.getString(2), cursor.getString(3),cursor.getString(4),cursor.getInt(5));
productList.add(product);
cursor.moveToNext();
}
cursor.close();
closeDatabase();
return productList;
}
}

我的 framgnet.class

 @Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mDBHelper = new DatabaseHelper(getActivity());

//Check exists database
File database = getActivity().getDatabasePath(DatabaseHelper.DBNAME);
if(false == database.exists()) {
mDBHelper.getReadableDatabase();
//Copy db
if(copyDatabase(getActivity())) {
Toast.makeText(getActivity(), "Copy database succes", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), "Copy data error", Toast.LENGTH_SHORT).show();
return;
}
}
// Get product list in db when db exists
mProductList = mDBHelper.getListWord();
// getCategoryFromDataBase();

}

private boolean copyDatabase(Context context) {
try {

InputStream inputStream = context.getAssets().open(DatabaseHelper.DBNAME);
String outFileName = DatabaseHelper.DBLOCATION + DatabaseHelper.DBNAME;
OutputStream outputStream = new FileOutputStream(outFileName);
byte[]buff = new byte[1024];
int length = 0;
while ((length = inputStream.read(buff)) > 0) {
outputStream.write(buff, 0, length);
}
outputStream.flush();
outputStream.close();
Log.w("MainActivity","DB copied");
return true;
}catch (Exception e) {
e.printStackTrace();
return false;
}
}

最初,我尝试在Activity.class中调用读取数据;它运行良好,但是当我在 Fragment 中尝试它时,它不会运行和显示

java.io.FileNotFoundException: /data/data/com.gnirt69.sqlitefromassetexample/databases/db3000.sqlite: open failed: EACCES (Permission denied)

这里发生了什么?
请帮助我。

最佳答案

您需要在 list 文件中添加以下权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

关于android - 从 Assets 文件夹错误读取sqlite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35862600/

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