我的应用不断删除我打开的sqlite db evrytime

时间:2019-05-06 22:06:34

标签: java android sqlite

每次我运行我的应用程序时,sqlite数据库都会被删除。 我做了一次登录和注册活动,第一次运行我的应用程序时,用户注册并登录没有问题。第二次,我注册的用户不在我的数据库中。

public static final String DBNAME = "DBSQLITE.db";
public static final String DBLOCATION = "/data/data/com.user.app/databases/";

private Context context;
private SQLiteDatabase database;

public Database(Context context) {
    super(context, DBNAME, null, 1);
    this.context = context;
}


@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

public void openDatabase() throws SQLException {

    String dbPath = context.getDatabasePath(DBNAME).getPath();
    if (database != null && database.isOpen()) {

        return;
    }

    database = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);
}

public void closeDatabase() {
    if (database != null) {
        database.close();
    }
}

2 个答案:

答案 0 :(得分:0)

在我的DatabaseHelper对象中,我要做的是检查应用程序的databases文件夹中是否已经存在一个数据库,如果存在,则不执行任何操作。但是,如果数据库不存在,那么我将一个空数据库从Assets文件夹复制到应用程序的databases文件夹。这是唯一一次清除应用程序数据库的时间。这是我的代码:

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseHelper extends SQLiteOpenHelper{

//The Android's default system path of your application database.
private static String DB_PATH = "/data/data/com.mycompany.myapp/databases/";
//DB Name in the Assets folder 
private static String DB_IN = “MyApp.sql”;
//DB name I want in the app's databases folder
private static String DB_NAME = “MyApp.sql";

private SQLiteDatabase myDataBase; 

private final Context myContext;

/**
 * Constructor
 * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
 * @param context
 */
public DataBaseHelper(Context context) {

    super(context, DB_NAME, null, 1);
    this.myContext = context;
}   

/**
 * Creates a empty database on the system and rewrites it with your own database.
 * */
public void createDataBase() throws IOException{

    //Before you create an empty database, see if it already exists
    boolean dbExist = checkDataBase();

    if(dbExist){
        //do nothing - database already exist
    }else{

        //By calling this method an empty database will be created into the default system path
        //of your application so we are going to be able to overwrite that database with our database.
        this.getReadableDatabase();

        try {
            // now, overwrite the database that was just created above
            // with an empty copy from the Assets folder
            copyDataBase();

        } catch (IOException e) {

            throw new Error("Error copying database");

        }
    }

}

/**
 * Check if the database already exist to avoid re-copying the file each time you open the application.
 * @return true if it exists, false if it doesn't
 */
private boolean checkDataBase(){

    SQLiteDatabase checkDB = null;

    try{
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);

    }catch(SQLiteException e){

        //database does't exist yet.

    }

    //If checkDB != null, that means we were able to open it with the openDatabase statement
    if(checkDB != null){

        checkDB.close();

    }

    return checkDB != null ? true : false;
}

/**
 * Copies your database from your local assets-folder to the just created empty database in the
 * system folder, from where it can be accessed and handled.
 * This is done by transferring the byte stream.
 * */
private void copyDataBase() throws IOException{

    //Open your local db as the input stream
    InputStream myInput = myContext.getAssets().open(DB_IN);

    // Path to the just created empty db
    String outFileName = DB_PATH + DB_NAME;

    //Open the empty db as the output stream
    OutputStream myOutput = new FileOutputStream(outFileName);

    //transfer bytes from the input file to the output file
    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_READWRITE);

}

@Override
public synchronized void close() {

    if(myDataBase != null)
        myDataBase.close();

    super.close();

}

@Override
public void onCreate(SQLiteDatabase db) {

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

}

答案 1 :(得分:0)

我想我终于解决了。我从未在所有正在使用的活动中复制数据库,因此,从某种意义上说,数据库被删除了。我只是在我的活动中称呼我的数据库,并且至少现在就可以正常工作了。