【问题标题】:Items in database sqlite are not updating (Xamarin.Forms)数据库 sqlite 中的项目未更新 (Xamarin.Forms)
【发布时间】:2020-07-19 03:23:52
【问题描述】:

我在 Visual Studio 中使用 Xamarin.Forms,并且我有一个我在表格中插入的产品列表。当我尝试更新 Cantidad、Precio 或其他功能时,Articulo 不会更新,并且列表会像以前一样显示。我正在尝试将 PK 用作字符串,因此当我尝试更新项目时,我需要 Articulo 的 Producto,它会验证 Producto 的 Id 是否存在于数据库中,然后进行更新。

这是我的模型 Articulo.cs

using SQLite;
using System;
using System.Collections.Generic;
using System.Text;

namespace Saansa.Modelos
{
    public class Articulo
    {
        [PrimaryKey,AutoIncrement]
        public string Id { get; set; }
        public string Producto { get; set; }
        public int Precio { get; set; }
        public int Cantidad { get; set; }
        public string MasterCategory { get; set; }
        public string Category1 { get; set; }
        public string Category2 { get; set; }
        public string Category3 { get; set; }
    }
}

这是我的 xalm 文件:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:local="clr-namespace:Saansa"
             x:Class="Saansa.Inventario"
             Title="Inventario">

            <StackLayout BackgroundColor="#f5cda2">
                <Image Margin="0,0,0,0" HeightRequest="10" Source="inventario.png" ></Image>
                <Label Margin="0,0,0,0" Text="Mi Cafe Delicias" FontAttributes="Bold"
                       FontSize="Large" TextColor="#b27b4b" HorizontalTextAlignment="Center" ></Label>
                <Entry x:Name="txtProducto" Placeholder="Código Producto"></Entry>
                <Entry x:Name="txtNombre" Placeholder="Nombre del producto"></Entry>
                <Entry x:Name="txtPrecio" Placeholder="Precio del producto"></Entry>
                <Entry x:Name="txtCantidad" Placeholder="Cantidad del Producto"></Entry>
                <Entry x:Name="txtMainCategory" Placeholder="Categoria general"></Entry>
                <Entry x:Name="txtSub1" Placeholder="Subcategoria 1"></Entry>
                <Entry x:Name="txtSub2" Placeholder="Subcategoria 2"></Entry>
                <Entry x:Name="txtSub3" Placeholder="Subcategoria 3"></Entry>
        <StackLayout  HorizontalOptions="CenterAndExpand" Orientation="Horizontal">
                    <Button x:Name="btnAdd" WidthRequest="200" Text="Añadir" Clicked="BtnAdd_Clicked"
                            BackgroundColor="White" BorderColor="#b27b4b" BorderWidth="3" CornerRadius="15"/>
                    <Button x:Name="btnRead" WidthRequest="200" Text="Buscar" Clicked="BtnRead_Clicked"
                            BackgroundColor="White" BorderColor="#b27b4b" BorderWidth="3" CornerRadius="15"/>
                </StackLayout>
                <StackLayout HorizontalOptions="CenterAndExpand" Orientation="Horizontal">
                    <Button x:Name="btnUpdate" WidthRequest="200" Text="Actualizar" Clicked="BtnUpdate_Clicked"
                            BackgroundColor="White" BorderColor="#b27b4b" BorderWidth="3" CornerRadius="15"/>
                    <Button x:Name="btnDelete" WidthRequest="200" Text="Borrar" Clicked="BtnDelete_Clicked"
                            BackgroundColor="White" BorderColor="#b27b4b" BorderWidth="3" CornerRadius="15"/>
                </StackLayout>
                <ListView x:Name="lstArticulo" BackgroundColor="#f5cda2">
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <ViewCell>
                                <StackLayout>
                                    <Label Text="{Binding Producto}"/>
                                    <Label Text="{Binding Id}" HorizontalOptions="EndAndExpand"
                                           TextColor="Black"/>
                                </StackLayout>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </StackLayout>
</ContentPage>

在我的 Inventario.xalm.cs 中,我的更新选项是这样的:

 private async void BtnUpdate_Clicked(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txtProducto.Text))
            {
                Modelos.Articulo articulo = new Modelos.Articulo()
                {
                    Id = txtProducto.Text,
                    Producto = txtNombre.Text,
                    Precio = Convert.ToInt32(txtPrecio.Text),
                    Cantidad = Convert.ToInt32(txtCantidad.Text),
                    MasterCategory = txtMainCategory.Text,
                    Category1 = txtSub1.Text,
                    Category2 = txtSub2.Text,
                    Category3 = txtSub3.Text
                };

                //Update Person
                await App.SQLiteDb.SaveItemAsync(articulo);

                txtNombre.Text = string.Empty;
                txtCantidad.Text = string.Empty;
                txtProducto.Text = string.Empty;
                txtPrecio.Text = string.Empty;
                txtMainCategory.Text = string.Empty;
                txtSub1.Text = string.Empty;
                txtSub2.Text = string.Empty;
                txtSub3.Text = string.Empty;
                await DisplayAlert("Success", "Person Updated Successfully", "OK");
                //Get All Persons
                var articuloLista = await App.SQLiteDb.GetItemsAsync();
                if (articuloLista != null)
                {
                    lstArticulo.ItemsSource = articuloLista;
                }

            }
            else
            {
                await DisplayAlert("Required", "Please Enter Nombre articulo", "OK");
            }
        }


还有我的 SQLiteHekper.cs:

public Task<int> SaveItemAsync(Modelos.Articulo articulo)
        {   //articulo.Cantidad. !string.IsNullOrEmpty(articulo.Cantidad)
            if (!string.IsNullOrEmpty(articulo.Id))
            {
                //probando esto 
                //var nuevoArticulo = GetItemAsync(articulo.Producto);
                return db.UpdateAsync(articulo);
            }  
            else
            {
                return db.InsertAsync(articulo);
            }
        }

【问题讨论】:

  • db.UpdateAsync(...)的返回值是多少?
  • 它必须是对象的列,具有我刚刚放置的新值,除了它的PK,但目前返回值不起作用,就像函数不更新列一样.
  • 它应该返回更新的行数。如果它返回 0,那么你传递的对象有问题
  • [PrimaryKey,AutoIncrement] public string Id { get;放;字符串的自动增量将不起作用。你应该得到 sql 错误。
  • 这并没有给我一个错误,但我理解你为什么这么说,问题是它没有搜索表并找到 PK。我将 PK 更改为 NotNull 并且它也不起作用。我尝试使用本教程:link 但在这里他们使用 PK 作为 int,我尝试将其用作字符串。那是我的问题

标签: c# sqlite xamarin.forms


【解决方案1】:

错误是它没有获取对象,这就是为什么我们首先需要调用它然后编辑我们想要更新的 Articulo。

Private async void BtnUpdate_Clicked(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(txtNombre.Text))
        {
            Modelos.Articulo articulo = await App.SQLiteDb.GetItemAsync(txtNombre.Text);

            //Aparentemente dejar el texto vacío no es equivalente a "", por eso el +"0"
            articulo.Cantidad = (string.Equals("0", txtCantidad.Text + "0")) ? articulo.Cantidad : Convert.ToInt32(txtCantidad.Text);
            articulo.Category1 = (string.Equals("0", txtSub1.Text + "0")) ? articulo.Category1 : txtSub1.Text;
            articulo.Category2 = (string.Equals("0", txtSub2.Text + "0")) ? articulo.Category2 : txtSub2.Text;
            articulo.Category3 = (string.Equals("0", txtSub3.Text + "0")) ? articulo.Category3 : txtSub3.Text;
            articulo.MasterCategory = (string.Equals("0", txtMainCategory.Text + "0")) ? articulo.MasterCategory : txtMainCategory.Text;
            articulo.Precio = (string.Equals("0", txtPrecio.Text + "0")) ? articulo.Precio : Convert.ToInt32(txtPrecio.Text);
            articulo.Costo = (String.Equals("0", txtCosto.Text + "0")) ? articulo.Costo : Convert.ToInt32(txtCosto.Text);
            articulo.Producto = (string.Equals("0", txtProducto.Text + "0")) ? articulo.Producto : txtNombre.Text;

            await App.SQLiteDb.SaveItemAsync(articulo);

            txtNombre.Text = string.Empty;
            txtCantidad.Text = string.Empty;
            txtProducto.Text = string.Empty;
            txtCosto.Text = string.Empty;
            txtPrecio.Text = string.Empty;
            txtMainCategory.Text = string.Empty;
            txtSub1.Text = string.Empty;
            txtSub2.Text = string.Empty;
            txtSub3.Text = string.Empty;
            await DisplayAlert("Success", "Producto actualizado con éxito", "OK");


        }
        else
        {
            await DisplayAlert("Required", "Please Enter código articulo", "OK");
        }
    }

这使得先带对象,检查是否某些数据不在条目中,让它保持原样,最后更新对象。

【讨论】:

    猜你喜欢
    • 2020-02-24
    • 2012-10-27
    • 1970-01-01
    • 2021-06-22
    • 2018-07-25
    • 1970-01-01
    • 2012-05-09
    • 1970-01-01
    相关资源
    最近更新 更多