第一个问题:我有一个要读取的数据库,并且我很确定它正在正确加载,因为我没有收到错误消息,除非我第一次尝试加载它但每隔一次加载时都没有错误,这是为什么呢?这会以任何方式影响设备上的应用程序吗?
第二个问题:当我加载应用程序然后单击后退按钮离开应用程序时,我收到此错误,
08-28 22:58:16.504: E/SQLiteDatabase(361): close() was never explicitly called on database '/data/data/com.example.databaseexample/databases/library_dev.db'
我在代码中的某个地方没有正确关闭吗?预先感谢您的任何帮助。
代码:
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DataBaseHelper myDbHelper = new DataBaseHelper(this);
try{
myDbHelper.createDataBase();
}catch(IOException ioe){
throw new Error("You f'd up, the database wasn't created *crys*");
}
try{
myDbHelper.openDatabase();
}catch(SQLException sqle){
throw sqle;
}
}
public void createListFromDB() {
// TODO Auto-generated method stub
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
这是我使用的其他课程:
public class DataBaseHelper extends SQLiteOpenHelper{
private static String DB_PATH = "/data/data/com.example.databaseexample/databases/";
private static String DB_NAME = "library_dev.db";
private SQLiteDatabase myDataBase;
private final Context myContext;
public DataBaseHelper(Context c){
super(c, DB_NAME, null, 1);
this.myContext = c;
}
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing since the database already exist.
}
else{
this.getReadableDatabase();
try{
copyDataBase();
}catch(IOException e){
throw new Error("Error Copying Database");
}
}
}
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database doesn't exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase()throws IOException{
//Opens your local db as the input stream.
InputStream myInput = myContext.getAssets().open(DB_NAME);
//Path to the just created empty DB.
String outFileName = DB_PATH + DB_NAME;
//Opens the empty DB as the output stream.
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfiles.
byte[] buffer = new byte[1024];
int length;
while((length = myInput.read(buffer)) > 0){
myOutput.write(buffer, 0, length);
}
//Close the Streams.
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDatabase() throws SQLException{
//Open the database.
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close(){
if(myDataBase != null){
myDataBase.close();
}
}
@Override
public void onCreate(SQLiteDatabase db){
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
}
}