我使用下面的DBHelper类将sqlite
db从 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
都有---rwxr-x
权限。
如何摆脱这个问题?
答案 0 :(得分:1)
使用SQLiteAssetHelper
类来处理数据库。它可以帮助您使用数据库而不会弄乱文件权限。
可以找到它的jar文件here。下载&amp;将其导入您的项目。
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;
}
答案 1 :(得分:0)
您是否正在更改数据库的结构?
如果您有更改,通常我会在运行新版本之前卸载该应用程序。运行新应用程序不会删除数据库,但卸载却会删除。您也可以使用adb命令'adb uninstall yourpackagename'
希望这会有所帮助。