title: SQLite data loss with android
body: <p>I'm using sqlite to save data locally when there is no internet connection. So far, everything has been working well but I'm getting feedback from some of the users that there is data loss while using the app, so the pending requests they saved just disappear. Asked them as well if this occured when the app was restarted or if a crash happened, but they said no.</p>
  
  <p>What could be the reason why the data suddenly disappeared?</p>
  
  <p>Passing the data</p>
  
  <pre><code>ContentValues values = new ContentValues();
  values.put(RequestsTable.COLUMN_USER_ID,userId);
  values.put(RequestsTable.COLUMN_REQUEST_TYPE,RequestsTable.REQUEST_PATROL);
  values.put(RequestsTable.COLUMN_REQUEST_BODY, patrolJson);
  
  return  RequestsDataSource.insertRequest(HandleActivity.this, values);
  </code></pre>
  
  <p>insert to database</p>
  
  <pre><code>public static synchronized long insertRequest(Context ctx,
                                                 ContentValues values) {
          return DataAccessWrapper.insert(database, ctx, RequestsTable.TABLE_REQUESTS,
                  RequestsTable.COLUMN_ID, values);
      }
  </code></pre>
  
  <p>insert function</p>
  
  <pre><code>// Insert a row in a table
      public static synchronized long insert(SQLiteDatabase database, Context ctx, String TABLE_NAME, String PRIMARY_KEY, ContentValues values) {
          long insertId = -1;
          try {
              insertId = database.insertOrThrow(TABLE_NAME, null, values);
  
              // check if the PRIMARY_KEY is null
              Object key = null;
              if (PRIMARY_KEY != null) {
                  key = values.get(PRIMARY_KEY);
              }
  
          } catch (Exception e) {
              e.printStackTrace();
          }
          return insertId;
      }
  </code></pre>
  
  <p>table</p>
  
  <pre><code>public class RequestsTable {
      // Database table, the columns are created in the same order
      public static final String TABLE_REQUESTS = "Requests";
      public static final String COLUMN_ID = "_id";
      public static final String COLUMN_REQUEST_TYPE = "RequestType";
      public static final String COLUMN_USER_ID= "UserId";
      public static final String COLUMN_REQUEST_BODY = "RequestBody";
      public static final String COLUMN_REQUEST_TIME = "RequestTime";
      public static final String COLUMN_LOG = "RequestLog";
  
      public static final String REQUEST_INCIDENT = "IncidentRequest";
      public static final String REQUEST_PATROL = "PatrolRequest";
      public static final String REQUEST_LOG = "LogRequest";
  
  
      // Database creation SQL statement
      private static final String TABLE_COLUMNS = " ("+ COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
              + COLUMN_REQUEST_TYPE + " TEXT, "
              + COLUMN_USER_ID + " INTEGER, "
              + COLUMN_REQUEST_TIME + " DATETIME DEFAULT (DATETIME(CURRENT_TIMESTAMP, 'LOCALTIME')), "
              + COLUMN_LOG + " TEXT, "
              + COLUMN_REQUEST_BODY + " TEXT " + " )";
  
      public static void create(SQLiteDatabase database) {
          DataAccessWrapper.create(database, TABLE_REQUESTS, TABLE_COLUMNS);
      }
  
      public static void onUpgrade(SQLiteDatabase database, int oldVersion,
                                   int newVersion) {
          DataAccessWrapper.upgrade(database, TABLE_REQUESTS, TABLE_COLUMNS, oldVersion,
                  newVersion);
      }
  
      public static void removeTable(SQLiteDatabase db) {
          DataAccessWrapper.removeTable(db, TABLE_REQUESTS);
      }
  
      public static void clearTable(SQLiteDatabase db) {
          DataAccessWrapper.clearTable(db, TABLE_REQUESTS);
      }
  }
  </code></pre>