目前我收到的错误消息是无法打开该文件。我试图做的就是打开我的数据库,打开我的表并从中提取信息。以下是我到目前为止:
public class MainActivity extends Activity
{
private static final String DATABASE_NAME = "ExerciseDatabase.db";
private static final String DATABASE_TABLE = "ExerciseList";
SQLiteDatabase myDatabase;
private void openDatabase(){
myDatabase = openOrCreateDatabase("/databaseManipulation/assets/" + DATABASE_NAME, Context.MODE_PRIVATE, null);
}
protected void onCreate(Bundle savedInstanceState) {
openDatabase();
Cursor allData = myDatabase.rawQuery("SELECT * FROM ExerciseList", null);
Log.e("TESTLOG", allData.toString());
}
}
根据我的阅读,似乎建议创建一个新数据库并用现有数据库覆盖它,任何帮助都会很棒
答案 0 :(得分:0)
添加此功能
public void CopyDB(InputStream inputStream,
OutputStream outputStream) throws IOException {
//---copy 1K bytes at a time---
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
}
使用此代码
try {
String destPath = "/data/data/" + getPackageName() +
"/databases";
File f = new File(destPath);
if (!f.exists()) {
f.mkdirs();
f.createNewFile();
//---copy the db from the assets folder into
// the databases folder---
CopyDB(getBaseContext().getAssets().open("MyDB"),
new FileOutputStream(destPath + "/MyDB"));
Log.i("esfandune", "esfandune");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
答案 1 :(得分:0)
您可以尝试这样的事情
public class SystemHelper{
private final String DB_PATH;
private final String DB_NAME;
private final Context mContext;
private SQLiteDatabase mDataBase;
public SystemHelper(Context context) {
super(context, "YOUR_DB_NAME_IN_ASSETS.db", null, 1);
DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
DB_NAME = "YOUR_DB_NAME_IN_ASSETS.db";
mContext = context;
createDataBase();
openDataBase();
}
public void createDataBase() {
try {
boolean mDataBaseExist = checkDataBase();
this.getReadableDatabase();
this.close();
if (!mDataBaseExist) {
Files.copyFromAssets(mContext, DB_NAME, DB_PATH);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private boolean checkDataBase() {
File dbFile = new File(DB_PATH + DB_NAME);
return dbFile.exists();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
@Override
public SQLiteDatabase getWritableDatabase() {
return mDataBase;
}
@Override
public synchronized void close() {
if (mDataBase != null)
mDataBase.close();
super.close();
}
public void openDataBase() {
String mPath = DB_PATH + DB_NAME;
mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.OPEN_READWRITE);
}
}
和Files.copyFromAssets(mContext,DB_NAME,DB_PATH)方法:
public static void copyFromAssets(Context context, String fileName, String path) {
InputStream mInput = null;
try {
mInput = context.getAssets().open(fileName);
File f = new File(path + fileName);
if (!f.exists())
f.createNewFile();
OutputStream mOutput = new FileOutputStream(f);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer)) > 0) {
mOutput.write(mBuffer, 0, mLength);
}
mOutput.flush();
mOutput.close();
mInput.close();
} catch (IOException e) {
e.printStackTrace();
}
}