我有一个预先填充的sqlite数据库,我已经在SQLiteStudio中创建并放入assets /目录。我尝试使用扩展SQLiteOpenHelper的DataBaseHelper将其复制到根存储中。
DataBaseHelper.java
private static String DBPATH = null;
private static String DBNAME = "test.sqlite";
private Context context;
private SQLiteDatabase db;
public DataBaseHelper(Context context) {
super(context, DBNAME, null, 1);
if (Build.VERSION.SDK_INT >= 17) {
DBPATH = context.getApplicationInfo().dataDir + "/databases/";
} else {
DBPATH = "/data/data/" + context.getPackageName() + "/databases/";
}
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
private boolean checkDataBase() {
File dbFile = context.getDatabasePath(DBNAME);
Log.v(TAG, "checkDataBase() - " + dbFile + " "+ dbFile.exists());
return dbFile.exists();
}
public void createDataBase()
throws Exception {
boolean exists = checkDataBase();
if(!exists) {
this.getReadableDatabase();
this.close();
try {
copyDatabase();
Log.e(TAG, "createDatabase() - database created");
} catch (IOException e) {
throw new Error("Cannot copy file");
}
}
}
private void copyDatabase()
throws IOException {
String path = DBPATH + DBNAME;
OutputStream dbStream = new FileOutputStream(context.getDatabasePath(DBNAME));
InputStream dbInputStream =
context.getAssets().open(DBNAME);
byte[] buffer = new byte[1024];
int length;
while((length = dbInputStream.read(buffer)) > 0) {
dbStream.write(buffer, 0, length);
}
dbInputStream.close();
dbStream.flush();
dbStream.close();
}
public boolean openDataBase() throws SQLException
{
String path = DBPATH + DBNAME;
db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.CREATE_IF_NECESSARY);
return db != null;
}
@Override
public synchronized void close()
{
if(db != null)
db.close();
super.close();
}
DataSource.java
private final Context context;
private SQLiteDatabase db;
private DataBaseHelper dbHelper;
public DataSource(Context context) {
this.context = context;
dbHelper = new DataBaseHelper(context);
}
public DataSource createDatabase() throws SQLException {
try {
dbHelper.createDataBase();
}
catch (Exception e) {
Log.e(TAG, e.toString() + " UnableToCreateDatabase");
throw new Error("UnableToCreateDatabase");
}
return this;
}
public DataSource open() throws SQLException {
try {
dbHelper.openDataBase();
db = dbHelper.getReadableDatabase();
}
catch (SQLException mSQLException) {
Log.e(TAG, "open >>"+ mSQLException.toString());
throw mSQLException;
}
return this;
}
public void close() {
dbHelper.close();
}
public List<String> listAllProducts() {
List<String> result = new ArrayList<String>();
try {
String MY_QUERY = ""
+ "SELECT Upper(prd_name) AS name "
+ "FROM products "
+ "WHERE prd_type = 'A' "
+ "ORDER BY name ASC";
Cursor cursor = db.rawQuery(MY_QUERY, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
result.add(cursor.getString(cursor.getColumnIndex("nome")));
cursor.moveToNext();
}
cursor.close();
} catch (Exception sqle) {
Log.e(TAG, "listAllProducts() >>"+ sqle.getMessage());
}
return result;
}
主要活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] array = null;
try {
DataSource ds = new DataSource(this);
ds.createDatabase();
ds.open();
List<String> items = ds.listAllProducts();
array = items.toArray(new String[items.size()]);
Log.i("ITEMS", Arrays.toString(array));
ds.close();
} catch (SQLException sql) {
Log.e("MAIN >>", sql.getMessage());
}
}
logcat的
com.test.myapp E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.Error: Cannot copy file
at com.test.myapp.db.DataBaseHelper.createDataBase(DataBaseHelper.java:106)
at com.test.myapp.db.DataSource.createDatabase(DataSource.java:30)
at com.test.myapp.MainActivity.onCreate(MainActivity.java:33)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
我该如何解决? 感谢