2016-07-05 77 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在客户端上遍历它们,但每个删除操作仍会有一个服务器请求。如果你想在客户端进行操作,那么我会推荐一些性能测试。 –