gpt4 book ai didi

java - 无法在android中读取复制的数据库

转载 作者:行者123 更新时间:2023-12-01 12:37:55 25 4
gpt4 key购买 nike

我使用下面的 DBHelper 类将 sqlite 数据库从 assets 复制到 sdcard。

它也可以在模拟器和其他 Android 设备上完美复制数据库,但是当我尝试从任何 Android 设备通过应用程序访问数据库时,应用程序会崩溃,而在模拟器上则工作正常。

DBHelper.java:-

public class DBHelper extends SQLiteOpenHelper {

public SQLiteDatabase database = null;
public File databaseFile;
public static String databaseName = "Db1.sqlite";
public String databasePath = "";

Context mContext;

public DBHelper(Context paramContext) {

super(paramContext, databaseName, null, 1);
this.mContext = paramContext;

this.databasePath = (Environment.getExternalStorageDirectory().getAbsolutePath()+"/Android/data/"+databaseName);
this.databaseFile = new File(this.databasePath);
if (!this.databaseFile.exists())
try {
deployDataBase(DBHelper.databaseName, this.databasePath);
return;
} catch (IOException localIOException) {
localIOException.printStackTrace();
}
}

private void deployDataBase(String dbNAme, String dbPath)
throws IOException {
InputStream localInputStream = this.mContext.getAssets().open(dbNAme);
FileOutputStream localFileOutputStream = new FileOutputStream(dbPath);
byte[] arrayOfByte = new byte[1024];
while (true) {
int i = localInputStream.read(arrayOfByte);
if (i <= 0) {
localFileOutputStream.flush();
localFileOutputStream.close();
localInputStream.close();
return;
}
localFileOutputStream.write(arrayOfByte, 0, i);
}
}

@Override
public synchronized void close() {

if (database != null)
database.close();

super.close();

}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub

}

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

}


public List<String> getAllColleges(){
List<String> colleges = new ArrayList<String>();

// Select All Query
String selectQuery = "SELECT * FROM colleges_list";

SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
colleges.add(cursor.getString(1));
} while (cursor.moveToNext());
}

// closing connection
cursor.close();
db.close();

// returning colleges
return colleges;
}

LogCat适用于模拟器以外的设备:-

08-20 18:14:43.359: E/AndroidRuntime(24460): FATAL EXCEPTION: main
08-20 18:14:43.359: E/AndroidRuntime(24460): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sqlite/com.crm.Quote_Details}: android.database.sqlite.SQLiteException: no such table: colleges_list: , while compiling: SELECT * FROM colleges_list
08-20 18:14:43.359: E/AndroidRuntime(24460): at dalvik.system.NativeStart.main(Native Method)
08-20 18:14:43.359: E/AndroidRuntime(24460): Caused by: android.database.sqlite.SQLiteException: no such table: colleges_list: , while compiling: SELECT * FROM colleges_list
08-20 18:14:43.359: E/AndroidRuntime(24460): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
08-20 18:14:43.359: E/AndroidRuntime(24460): at com.crm.DBHelper.getAllColleges(DBHelper.java:93)

我还注意到一件事,模拟器中复制的db具有rwxrwx---权限,而复制的db在模拟器中具有rwxrwx---权限任何设备都具有 ---rwxr-x 权限。

如何解决这个问题?

最佳答案

使用SQLiteAssetHelper类来处理数据库。它将帮助您使用数据库而不会弄乱文件权限。

可以找到它的jar文件here 。下载并将其导入您的项目中。

public class DBHelper extends SQLiteAssetHelper {

private static final String DATABASE_NAME = "Db1.sqlite";
private static final int DATABASE_VERSION = 1;

public AssetsHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}


public List<String> getAllColleges(){
List<String> colleges = new ArrayList<String>();

// Select All Query
String selectQuery = "SELECT * FROM colleges_list ORDER BY Organization_Name";

SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);

// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
colleges.add(cursor.getString(1));
} while (cursor.moveToNext());
}

// closing connection
cursor.close();
db.close();

// returning colleges
return colleges;
}

关于java - 无法在android中读取复制的数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25405382/

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