首页 > 解决方案 > Xamarin,我如何访问 sqlite 中的特定列属性以便添加值?

问题描述

我知道如何读取或显示数据库中插入的所有数据,但我不知道如何查询特定数据。在我的数据库位于 Post.cs 中,它将存储日期/时间并从用户那里获取 2 个数据。
像这样:在此处输入图像描述

Column1(ID), Column2(Date), (column3(rain1), column4(rain2).
1              12/13/2019         21               22
2              12/16/2019         21               22
3              12/16/2019         11               12

我想做:如果 (rain1 && rain2) 有相同的日期。我想从rain1中添加2个数据,即21 + 11并将总数保存到rain1total = 32,然后rain2total将是34。我不知道从哪里开始

这是 Post.cs

namespace listtoPDF.Model
{
//Post table, user posting drainvolume
//this is the source of Binding
public class Post: INotifyPropertyChanged
{
    private double? rain1Vol;
    private double? rain2Vol;
    //from settingspage to show up in history
    string rain1lbl = Settings.Drain1LocationSettings;
    string rain2lbl = Settings.Drain2LocationSettings;      

    //ID primary key that we will autoincrement
    //These are columns ID, drain1 to 8 so 9 columns
    //These are binding source for Historypage
    [PrimaryKey, AutoIncrement]
    public int ID { get; set; }

    public static bool showLabel { get; set; }  //public class model

    public string rain1Lbl
    {
        get => rain1lbl;
        set => rain1lbl= Settings.Drain1LocationSettings;    
    }

    public string rain2Lbl
    {
        get => rain2lbl;
        set => rain2lbl= Settings.Drain2LocationSettings;    
    }

    public string CDateTime { get; set; }

    [MaxLength(3)]
    public double? rain1vol
    {
        get { return rain1Vol; }
        set
        {
            rain1Vol = value;
            RaisePropertyChanged("rain1vol");
        }
    }
    [MaxLength(3)]
    public double? rain2vol
    {
        get { return rain2Vol; }
        set
        {
            rain2Vol = value;
            RaisePropertyChanged("rain2vol");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;

    void RaisePropertyChanged(string property)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(property));
        }

    }
}
}

这是列出日期输入数据的主要内容

namespace listtoPDF
{
public partial class MainPage : ContentPage
{
    List<Post> posts;


    public MainPage()
    {
        InitializeComponent();
    }

    protected override void OnAppearing()
    {
        base.OnAppearing();
        SQLiteConnection conn = new SQLiteConnection(App.DatabaseLocation);
        conn.CreateTable<Post>();
        posts = conn.Table<Post>().ToList();
        postListView.ItemsSource = posts;     
        conn.Close();
            //right click quickwatch

    }
    void addbuttonHandle_Clicked(object sender, EventArgs args)
    {
        Navigation.PushModalAsync(new AddRainsPage());

    }

    private void GetValues()
    {
        //Initiate SQLite connection
        //Create table with class
        //Get the values in table
        //close the line
        SQLiteConnection conn = new SQLiteConnection(App.DatabaseLocation);
        conn.CreateTable<Post>();
        posts = conn.Table<Post>().ToList();
        conn.Close();
    }       
}

这是我需要查询添加某个列的页面,以便我可以添加与其关联的数据。到目前为止,它正在连接到 sqlite。

namespace listtoPDF
{
public partial class selecteddataPage : ContentPage
{
    List<Post> posts;

    public selecteddataPage()
    {
        InitializeComponent();
    }

    private void GetValues()
    {
        //Initiate SQLite connection
        //Create table with class
        //Get the values in table
        //close the line
        SQLiteConnection conn = new SQLiteConnection(App.DatabaseLocation);
        conn.CreateTable<Post>();
        posts = conn.Table<Post>().ToList();

        **if (Post.rain1 values have the same date )
          {
             totalrain1= then add data that have same date         
          }
         if (Post.rain2 values have the same date )
          {
             totalrain1= then add data that have same date         
          }**

        conn.Close();

    }
}
}

标签: sqlitexamarinxamarin.forms

解决方案


推荐阅读