title: SQLite.SQLiteException: database disk image is malformed in xamarin.forms application
body: <p>I have created a xamarin.forms application and ship a sqlite database with the application, im getting the following error:</p>
  
  <p>database disk image is malformed.</p>
  
  <p>This is the stacktrace:</p>
  
  <pre><code>SQLite3.Prepare2 (SQLitePCL.sqlite3 db, System.String query)
  SQLiteCommand.Prepare ()
  SQLiteCommand+&lt;ExecuteDeferredQuery&gt;d__12`1[T].MoveNext ()
  List`1[T].AddEnumerable (System.Collections.Generic.IEnumerable`1[T] enumerable)
  System.Collections.Generic.List`1[T]..ctor (System.Collections.Generic.IEnumerable`1[T] collection) [0x00062] in &lt;d4a23bbd2f544c30a48c44dd622ce09f&gt;:0
  Enumerable.ToList[TSource] (System.Collections.Generic.IEnumerable`1[T] source)
  SQLiteCommand.ExecuteQuery[T] ()
  SQLiteConnection.Query[T] (System.String query, System.Object[] args)
  SQLiteAsyncConnection+&lt;&gt;c__DisplayClass28_0`1[T].&lt;QueryAsync&gt;b__0 ()
  Task`1[TResult].InnerInvoke ()
  Task.Execute ()
  MyApp+&lt;LoadDocFromDB&gt;d__18.MoveNext ()
  VolumeView+&lt;VolumesListView_ItemSelected&gt;d__13.MoveNext ()
  AsyncMethodBuilderCore+&lt;&gt;c.&lt;ThrowAsync&gt;b__6_0 (System.Object state)
  SyncContext+&lt;&gt;c__DisplayClass2_0.&lt;Post&gt;b__0 ()
  Thread+RunnableImplementor.Run ()
  IRunnableInvoker.n_Run (System.IntPtr jnienv, System.IntPtr native__this)
  (wrapper dynamic-method) System.Object.17(intptr,intptr)
  </code></pre>
  
  <p>this is happening on both Xamarin iOS and Xamarin Android.</p>
  
  <p>the xamarin forms versions i use are:
  4.3.0.947036</p>
  
  <p>i use the following sqlite packages:
  SQLiteNetExtensions version 2.1.0
  SQLiteNetExtensions.Async version 2.1.0</p>
  
  <p>this is the following code that is crashing:</p>
  
  <pre><code>public static async Task LoadDocFromDB(string volume, int chapterNr)
  {
      if(dbPath == null)
      {
          dbPath = await DependencyService.Get&lt;IDatabaseAccess&gt;().DatabasePathAsync(false,"Data");
      }
      var connection = new SQLiteAsyncConnection(dbPath, SQLiteOpenFlags.FullMutex | SQLiteOpenFlags.ReadWrite);
      IRepository&lt;Doc&gt; repository = new DocRepository&lt;Doc&gt;(connection);
      try
      {
          var docMetadata = await repository.QueryAsync($"SELECT * FROM [Docs] WHERE [volume] == '{volume}' AND [chapterNr] == '{chapterNr}'");
          LoadedDoc = await repository.GetWithChildrenAsync(docMetadata.First().chapterID);
          await repository.DisposeDbConnectionAsync();
      }
      catch (Exception ex)
      {
          throw;
      }
      await Task.FromResult(0);
  }
  </code></pre>
  
  <p>this is the DisposeDbConnectionAsync() method implementation:</p>
  
  <pre><code>public async Task DisposeDbConnectionAsync()
          {
              using (await _mutex.LockAsync()) //from the Nito.AsyncEx library
              {
                  if (dbConnection != null)
                  {
                      dbConnection.GetConnection().Close();
                      GC.Collect();
                      GC.WaitForPendingFinalizers();
                      dbConnection.GetConnection().Dispose();
                      SQLite.SQLiteAsyncConnection.ResetPool();
                      dbConnection = null;
                  }
              }
          }
  </code></pre>
  
  <p>This seem to happen sometimes, any clue what might be causing this?</p>
  
  <p>I have also run a Pragma integrity check on the database and its was ok, no issues.</p>
  
  <p>thanks for all the help!</p>