Android Azure删除所有行

本文关键字:删除 Azure Android | 更新日期: 2023-09-27 17:54:55

我正在构建一个Android应用程序与Azure数据库(在Xamarin),使用Azure移动服务。我想清除一个表的记录。
虽然有一张桌子。RemoveAsync',我不知道如何选择所有的行。
是用'Select'还是'Where'?我很感激你的帮助。

下面是我的代码:
    //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");
            }
        }
    }
}
}

谢谢。

Android Azure删除所有行

在客户端SDK中,您不能在本地SQLite存储上运行任意SQL语句。所以,你必须选择所有的行,然后循环删除它们。(使用ToListAsync将所有内容加载到内存时要小心,因为这可能会耗尽设备上的内存。)注意,如果您有X行,这将导致X个服务器请求,这可能非常慢。关于如何在客户端执行请求,请参阅本文:如何:在移动应用程序中删除数据。

如果您想要删除符合特定条件的所有行,您最好在服务器上编写自定义API,并通过调用API让客户端只发送一个请求。在服务器端,您可以使用SQL或LINQ,这取决于您使用的是。net还是Node.js。

例如,使用。net后端,您可以使用如何:定义自定义API控制器来创建自定义API。

在你的一个控制器方法中,你会有以下代码:

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