重新启动应用程序时,Android Sqlite插入的数据不会保留

时间:2014-03-04 21:45:17

标签: android sqlite

我见过类似的问题并提供了解决方案,但我仍遇到问题。我有一个sqlite数据库,在我的程序中,我执行插入操作:

dbHelper = MyDBHelper.getInstance(context);
SQLiteDatabase db = dbHelper.getWritableDatabase();             

ContentValues values = new ContentValues();
values.put("listname", "examplelist"); // inserting a string
values.put("productname", textPart); // inserting an int
values.put("productprice", pricePart); // inserting an int// Inserting Row
db.insert("locallist", null, values);

此时,当我查询数据库时,我看到该行已被插入。然后,我关闭我的应用程序并再次打开它,并在主要活动的onCreate方法中,我执行以下操作:

MyDBHelper dbHelper;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);
    dbHelper = MyDBHelper.getInstance(this);

    String [] searchResultsByListname = dbHelper.QueryByListName("examplelist");
}

但此查询返回一个空数组。这是MyDBHelper类:

public class MyDBHelper extends SQLiteOpenHelper {

private static MyDBHelper mInstance = null;
private static final int DB_VERSION = 5;
private static final String DB_NAME = "database.sqlite";
static final String TABLE_NAME = "PRODUCTS";
private final Context mContext;
private static SQLiteDatabase myWritableDb;

// private static String DATABASE_PATH;

public static MyDBHelper getInstance(Context ctx) {
    if (mInstance == null) {
        mInstance = new MyDBHelper(ctx.getApplicationContext());
        // DATABASE_PATH = ctx.getPackageName();
    }
    return mInstance;
}

private MyDBHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
    this.mContext = context;
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {

}

public void createDatabase() throws IOException {
    createDB();
}

private void createDB() throws IOException {

    boolean dbExist = DBExists();
    if (!dbExist) {
        this.getReadableDatabase();
        copyDBFromResource();
    }

}

private boolean DBExists() {
    File dbFile = new File(/* DATABASE_PATH + */DB_NAME);
    return dbFile.exists();
}

private void copyDBFromResource() throws IOException {
    InputStream inputStream = null;
    OutputStream outStream = null;
    String dbFilePath = DB_NAME;

    try {
        inputStream = mContext.getAssets().open(DB_NAME);
        outStream = new FileOutputStream(mContext.getDatabasePath(DB_NAME));

        byte[] buffer = new byte[1024];
        int length;
        while ((length = inputStream.read(buffer)) > 0) {
            outStream.write(buffer, 0, length);
        }

        outStream.flush();
        outStream.close();
        inputStream.close();
    } catch (Exception ex) {
        System.out.println(ex.getMessage());
    }
}




    public String[] QueryByListName(String listname) {
    SQLiteDatabase db = this.getWritableDatabase();
    String query = "select productname, productimage,productprice from locallist where listname = '"+listname+"'";
    Cursor cursor = db.rawQuery(query, null);

    String[] searchResult= new String[cursor.getCount()];

    String[] searchResultNoFound= new String[1];

    String searchResultSingleLine="";
    int index=0;
    while (cursor.moveToNext()) {
        for (int i = 0; i < cursor.getColumnCount(); i++) {
            searchResultSingleLine += cursor.getString(i) + ";";
        }
        searchResult[index]=searchResultSingleLine; index++;

        searchResultSingleLine="";

    }

    searchResultNoFound[0]="Urun Bulunamadi";

    if(cursor.getCount()>0)
        return searchResult;
    else
        return searchResultNoFound;
}
}

我在应用程序启动时检查了表是否存在,并且我确信该表存在,但它是空的。那么,为什么我的插入数据在应用关闭时不会被保留?任何人都可以帮助我吗?

注意:我的数据库“database”位于我的assets文件夹中,它有两个表:PRODUCTS和locallist。 PRODUCTS表已预先填充,我可以毫无问题地使用它,但locallist表在开头是空的,我在运行时添加数据。

由于

1 个答案:

答案 0 :(得分:1)

您的DBExists()方法不正确。你应该使用

return mContext.getDatabasePath(DB_NAME).exists();

截至目前,每次重新启动应用程序时,都会使用assets文件夹中的版本覆盖数据库。最后,您需要从this.getReadableDatabase();方法中删除createDB(),因为这是不必要的。