Я создаю приложение Android с базой данных Azure (в Xamarin), используя Azure Mobile Services. Я хочу очистить таблицу от ее записей.
Хотя есть «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");
}
}
}
}
}
Спасибо.
Спасибо. Не могли бы вы предоставить несколько более конкретных указаний о том, как это сделать (с кодом), поскольку я действительно новичок в этом и не знаю, что делать. Я использую C#, а не Node.js. Благодаря! – amitairos
Если я знаю, что мое число строк не будет больше, чем около 100, я все равно не буду их выбирать? Как мне это сделать? Благодаря? – amitairos
@amitairos Я обновил свой ответ на примере SQL, который вы напишете на сервере. Если у вас меньше 100 строк, вполне нормально их прокручивать на клиенте с помощью ToListAsync, но у вас все равно будет один запрос сервера для каждой операции удаления. Если вы хотите выполнить операцию на клиенте, я бы рекомендовал провести тестирование производительности. –