0

第一个问题:我有一个要读取的数据库,并且我很确定它正在正确加载,因为我没有收到错误消息,除非我第一次尝试加载它但每隔一次加载时都没有错误,这是为什么呢?这会以任何方式影响设备上的应用程序吗?

第二个问题:当我加载应用程序然后单击后退按钮离开应用程序时,我收到此错误,

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){

}

}
4

1 回答 1

1

问题 1

您遇到此错误是因为您第一次编写代码时试图打开一个尚不存在的数据库。在这种情况下,它会创建一个新数据库(从输入文件复制它)。所以是的,您将自从您第一次启动应用程序以来,每部手机都会出现此错误,在您创建它之前不会有数据库。顺便说一句,这是处理数据库的一种非常糟糕的方法。

问题2

发生这种情况是因为您没有明确关闭您的数据库。你应该在你的onDestroy()方法上做..你应该把它添加到你的DataBaseHelper

public void closeDatabase() throws SQLException {
    // Close the database.
    myDataBase.close();
}

这个方法应该在onDestroy()你的MainActivity. 我希望这可以帮助你

更新 您可以查看本教程,它很好地解释了如何处理 SQLite

于 2012-08-28T23:27:37.837 回答