关闭应用程序时,将删除Android 4.4中的Sqlite

时间:2015-03-10 13:42:52

标签: android sqlite

我的应用程序中出现了sqlite问题。问题是:关闭应用程序,在eclipse工作室中重新安装应用程序后,我的所有数据的sqlite都消失了。我使用设备上的后退箭头关闭应用程序,或者从照片上显示的经理栏中滑出此应用程序的图标。当我向listView添加新位置时,它工作正常但是当我从管理器栏中刷出应用程序时它会破坏我的sqlite数据库而我会丢失所有内容。我将列表视图存储在全局对象中(扩展了Application)。这个消失的数据库存在问题。这有什么不对?我在平板电脑Samsung Galaxy Tab 4上使用Android 4.4。

PHOTO:

My app closing with "swipe-out" gesture

编辑:

我的DBHelper类

public class SqliteHelper extends SQLiteOpenHelper{

    public static final String TABLE_NAME = "gifs";
      public static final String NAME = "name";
      public static final String RATING = "rating";
      public static final String DATE = "date";
      public static final String URL = "url";
      public static final String COVERURL = "coverurl";
      public static final String MAIN_CONTENT_URL = "mainURL";

      public static final String  DATABASE_FILE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath()+"/storeUGif/";


      public static final String DATABASE_NAME = "items.db";
      private static final int DATABASE_VERSION = 1;

      private static final String DATABASE_CREATE = "create table " + TABLE_NAME + "(" + NAME + " text" + "," + RATING 
              + " int" + "," + URL + " text" + "," + DATE + " text" + "," + COVERURL + " text" + "," + MAIN_CONTENT_URL + " text" + ");";

    public SqliteHelper(Context context) {

        super(context,DATABASE_NAME, null, DATABASE_VERSION);
        //super(context,DATABASE_FILE_PATH + DATABASE_NAME, null, DATABASE_VERSION);
        if(GlobalApplicationState.DEBUG_MODE)
       System.out.println(DATABASE_FILE_PATH);

      }

    @Override
    public void onCreate(SQLiteDatabase database) {

         database.execSQL(DATABASE_CREATE);
    }

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

        database.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(database);

    }




}

插入方法:

public void instertItemToDataBase(GifModel model)
    {
        if(!dataBaseManager.isOpen())
        {
            dataBaseManager.open();
        }
        if(!demotywatorBaseManager.isOpen())
        {
            demotywatorBaseManager.open();
        }

        if(model.getMainLinkToTheContent().contains("http://kwejk.pl"))
        {
            dataBaseManager.insertGifToDataBase(model);

            //dataBaseManager.close();
        }
        else if(model.getMainLinkToTheContent().contains("http://demotywatory.pl"))
        {
            demotywatorBaseManager.insertGifToDataBase(model);
            //demotywatorBaseManager.close();
        }

    }

最后一个:

public void insertGifToDataBase(GifModel newGif) {

        //System.out.println(newGif.getMainLinkToTheContent());

        ContentValues values = new ContentValues();
        values.put(SqliteHelper.NAME, newGif.getNameOfIteme());
        values.put(SqliteHelper.RATING, newGif.getRating());
        values.put(SqliteHelper.URL, newGif.getUrl());
        values.put(SqliteHelper.DATE, newGif.getAddedDate());
        values.put(SqliteHelper.COVERURL, newGif.getCoverUrl());
        values.put(SqliteHelper.MAIN_CONTENT_URL, newGif.getMainLinkToTheContent());
        database.beginTransaction();
        database.insert(SqliteHelper.TABLE_NAME, null, values);
        database.endTransaction();

    }

编辑2:

public ArrayList<GifModel> getAllGifs() {

        ArrayList<GifModel> allGifs = new ArrayList<GifModel>();

        try
        {
            Cursor cursor = database.query(SqliteHelper.TABLE_NAME, allColumns, null, null, null, null, null);
            cursor.moveToFirst();  // Here exception shows up.
            while (!cursor.isAfterLast()) {
                GifModel gif = cursorToNewGif(cursor);
                allGifs.add(0,gif);
                cursor.moveToNext();
            }
            // make sure to close the cursor
            cursor.close();


        }catch(Exception e)
        {
            //System.out.println(e.getMessage());
            e.getCause();
        }
//      for(int i = 0; i < allGifs.size(); i++)
//      {
//          System.out.println("CUrosr"+allGifs.get(i).toString());
//      }
        return allGifs;
    }

2 个答案:

答案 0 :(得分:2)

您需要在setTransactionSuccessful()之前致电endTransaction()以实际应用更改。如果没有成功标志,则回滚事务。

答案 1 :(得分:2)

我不确切知道SqliteHelper是如何工作的,我手工创建数据库。但是,我认为:

您每次运行都会创建一个新表。请参阅DATABASE_CREATE字符串: 创建表x(foo int,bar int);

IF NOT EXISTS 添加到表创建表达式中。成功:

CREATE TABLE x IF NOT EXISTS(foo INT, bar INT);

这样,只有在表格尚未存在的情况下,您才能创建表格。 如果它存在,它就不会做任何事情。

在您的代码中:

private static final String DATABASE_CREATE = "create table " + TABLE_NAME + "if not exists (" + NAME + " text" + "," + RATING 
          + " int" + "," + URL + " text" + "," + DATE + " text" + "," + COVERURL + " text" + "," + MAIN_CONTENT_URL + " text" + ");";