2016-07-05 113 views
1

我正在使用Azure移動服務在Azure數據庫(在Xamarin中)構建Android應用程序。 我想從它的記錄中清除表格。
雖然有一個'table.RemoveAsync',我不知道如何選擇所有的行。
它與'選擇'或'在哪裏'? 我希望得到一些幫助。Android Azure刪除所有行

這裏是我的代碼:

//Mobile Service Client reference 
    private MobileServiceClient client; 

    //Mobile Service sync table used to access data 
    private IMobileServiceSyncTable<Mashlim> mashlimTable; 

    const string applicationURL = @"http://blahblah.azurewebsites.net/"; 


    public override async void OnCreate(Bundle savedInstanceState) 
    { 
     base.OnCreate(savedInstanceState); 

     CurrentPlatform.Init(); 

     // Create the Mobile Service Client instance, using the provided 
     // Mobile Service URL 
     client = new MobileServiceClient(applicationURL); 
     await InitLocalStoreAsync(); 

     // Get the Mobile Service sync table instance to use 
     mashlimTable = client.GetSyncTable<Mashlim>(); 

    } 

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
     var view = inflater.Inflate(Resource.Layout.ShabbatMinyan, container, false); 
     …. 

     OnRefreshItemsSelected(); 

     return view; 
    } 

    private async Task InitLocalStoreAsync() 
    { 
     // new code to initialize the SQLite store 
     string path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), localDbFilename); 

     if (!File.Exists(path)) 
     { 
      File.Create(path).Dispose(); 
     } 

     var store = new MobileServiceSQLiteStore(path); 
     store.DefineTable<Mashlim>(); 

     // Uses the default conflict handler, which fails on conflict 
     // To use a different conflict handler, pass a parameter to InitializeAsync. For more details, see http://go.microsoft.com/fwlink/?LinkId=521416 
     await client.SyncContext.InitializeAsync(store); 
    } 

    private async Task SyncAsync() 
    { 
     try 
     { 
      await client.SyncContext.PushAsync(); 
      await mashlimTable.PullAsync("allMashlims", mashlimTable.CreateQuery()); // query ID is used for incremental sync 
     } 
     catch (Java.Net.MalformedURLException) 
     { 
      CreateAndShowDialog(new Exception("There was an error creating the Mobile Service. Verify the URL"), "Error"); 
     } 
     catch (Exception e) 
     { 
      CreateAndShowDialog(e, "Error"); 
     } 
    } 

    // Called when the refresh menu option is selected 
    private async void OnRefreshItemsSelected() 
    { 
     await SyncAsync(); // get changes from the mobile service 
     await RefreshItemsFromTableAsync(); // refresh view using local database 
    } 

    //Refresh the list with the items in the local database 
    private async Task RefreshItemsFromTableAsync() 
    { 
     try 
     { 
      // Get the items that were marked as mashlim and add them to list 
      var list = await mashlimTable.Where(item => item.IsMashlim == true).ToListAsync(); 

      mashlimim = 0; 
      foreach (Mashlim current in list) 
       mashlimim++; 

      mashlimimNumText.Text = mashlimim.ToString(); 
     } 
     catch (Exception e) 
     { 
      CreateAndShowDialog(e, "Error"); 
     } 
    } 

    [Java.Interop.Export()] 
    public async void AddItem() 
    { 
     if (client == null) 
     { 
      return; 
     } 

     if(Settings.MashlimId==string.Empty) 
     { 

      // Create a new item 
      item = new Mashlim 
      { 
       Name = nameText.Text, 
       PhoneNumber = phoneText.Text, 
       IsMashlim = true 
      }; 


      try 
      { 
       await mashlimTable.InsertAsync(item); // insert the new item into the local database 
       await SyncAsync(); // send changes to the mobile service 
       await RefreshItemsFromTableAsync(); 
       Settings.MashlimId = item.Id; 
       Settings.MashlimName = item.Name; 
       Settings.IsMashlim = true; 
       Settings.MashlimPhone = item.PhoneNumber; 
      } 
      catch (Exception e) 
      { 
       CreateAndShowDialog(e, "Error"); 
      } 
     } 

     else 
     { 
      Settings.IsMashlim = true; 
      item = new Mashlim 
      { 
       Id = Settings.MashlimId, 
       Name = Settings.MashlimName, 
       IsMashlim = Settings.IsMashlim, 
       PhoneNumber = Settings.MashlimPhone 
      }; 


      try 
      { 
       await mashlimTable.UpdateAsync(item); // insert the new item into the local database 
       await SyncAsync(); // send changes to the mobile service 
       await RefreshItemsFromTableAsync(); 
       mashlim = true; 
      } 
      catch (Exception e) 
      { 
       CreateAndShowDialog(e, "Error"); 
      } 
     } 
    } 

} 
} 

感謝。

回答

2

在客戶端SDK中,您無法在本地SQLite存儲區上運行任意SQL語句。所以,你將不得不選擇所有的行並循環刪除。 (請注意,使用ToListAsync將所有內容加載到內存中,因爲可能會耗盡設備上的內存。)請注意,如果您有X行,則會導致X服務器請求,這可能非常緩慢。請參閱本文以瞭解如何在客戶端上執行請求:How to: Delete data in a mobile app

如果要刪除所有符合特定條件的行,最好在服務器上編寫自定義API,讓客戶端通過調用API發送一個請求。在服務器上,您可以使用SQL或LINQ,具體取決於您使用的是.NET還是Node.js.

例如,使用.NET後端,您可以使用How to: Define a custom API controller創建自定義API。

在你的控制方法之一,您將有以下代碼:

using (var context = new YourContext()) 
{ 
    context.ExecuteStoreCommand("DELETE FROM YOURTABLE WHERE Condition = value"); 
} 
+0

謝謝。請你提供一些關於如何做到這一點的具體指導(附代碼),因爲我真的很陌生,不確定要做什麼。我使用的是C#,而不是Node.js.謝謝! – amitairos

+0

如果我知道我的行數不會超過大約100個,我是不是應該全部選擇它們?我該怎麼做呢?謝謝? – amitairos

+1

@amitairos我已經用你在服務​​器上寫的SQL的例子更新了我的答案。如果您的行數少於100行,則可以使用ToListAsync在客戶端上遍歷它們,但每個刪除操作仍會有一個服務器請求。如果你想在客戶端進行操作,那麼我會推薦一些性能測試。 –