【问题标题】:How to get Data from SQLite base on interval如何根据间隔从 SQLite 获取数据
【发布时间】:2016-12-21 17:44:42
【问题描述】:

说,我有以下 SQLite DB:

数据类型:

DateTime                Int            
(with HH:MM:SS)         Queue Size

2016-08-15 06:10:10         20          
2016-08-15 06:11:12         30          
2016-08-15 06:12:16         10          
2016-08-15 06:13:12         10          
2016-08-15 06:14:10         60 

2016-08-15 06:15:08         20          
2016-08-15 06:16:10         30          
2016-08-15 06:17:12         10     
2016-08-15 06:18:11         10  
2016-08-15 06:19:12         10  

2016-08-15 06:20:12         10 

1) 如何使用 SQL 为图表创建数据源,每 5 分钟获取一次队列总数?

示例
第一个 5 分钟,总队列为:130
第 2 5 分钟,总 Queue 为:80

第三个5分钟?

2) I have this class for the Chart


public class Queue
{
    public DateTime  Interval { get; set; }
    public int Queue { get; set; }
}

private void LoadQueue()
{
  //-- how to use SQL to query the SQLite DB


  List<Queue> QList = new List<Queue>();


  //---code ---

 (ColumnChart.Series[0] as ColumnSeries).ItemsSource = QList;



}

图表的 XAML:

 <Charting:Chart x:Name="ColumnChart" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="505,100,0,0" Width="399" Height="400">

   <Charting:ColumnSeries Title="Queue" Margin="0" 

IndependentValuePath="Interval" 
DependentValuePath="Queue" IsSelectionEnabled="True"/>

    </Charting:Chart>

--------- 编辑

public class tblQueue
 {
     public DateTime QueueDate { get; set; }       
     public int QueueSize { get; set; }

 }

Case : insert the data into SQLite DB using SQlite.Net-PCL

    var db = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), DBPath);

      var newItem = new tblQueue()
                {
                   a) QueueDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                   b) QueueDate = DateTime.Now,

                    QueueSize = intQ_Length,

                };

                db.Insert(newItem);

Question :

1) what dataType should I use for my tblQueue class for table ?

which dataType to use?

1a)  public DateTime QueueDate { get; set; }   
1b)  public string QueueDate { get; set; }     

------- 编辑_2: 基于上述 SQLite DB:

1) 假设数据正常。这是我想在 SQL-Select 之后显示的结果:

Interval every 5 minute     ttl Queue
----------------------      ------------
2016-08-15 06:15:00          150  

2016-08-15 06:20:00          70

2016-08-15 06:25:00


 <Charting:Chart x:Name="ColumnChart" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="253,100,0,0" Width="651" Height="506">

<Charting:ColumnSeries Title="WaitingLine" Margin="0" IndependentValuePath="Interval" 
DependentValuePath="Size" IsSelectionEnabled="True"/>

</Charting:Chart>

我需要以下图表:

2.1))IndependentValuepath ="Interval" 示例:06:15

2.2)DependentValuePath ="大小:

DependentValuePath 显示 : 150 , 70 对于 y 轴

IndependentValuePath 显示 : 06:15 , 06:20 , 06:25 for x 轴

这些值(2.1 和 2.2)来自下面的类

3) 如何在这个 Val 类中添加 Interval 的属性?

public class Val
{
   ?? interval
   public int size { get; set; }
}

4)我似乎没有像(1)中那样按间隔得到上述数据的结果

private void LoadQueue()
{
    List<Val> ints=db.Query<Val>("SELECT sum(Queue) as 'size' from Queue group by (strftime('%Y%m%d%H0',QueueDate)+strftime('%M',QueueDate)/5)");  

}

请注意:我使用 QueueDate , tblQueue 中的 Queue,

谢谢

我不确定我的 XAML 是否正确。请帮忙。

谢谢。

【问题讨论】:

  • 谁能提供帮助?

标签: sqlite winrt-xaml uwp uwp-xaml


【解决方案1】:

如何使用 SQL 为图表创建数据源,每 5 分钟获取一次队列总数?

您可以将strftime('%M',Interval) 除以 5 分钟并按结果分组:

public class Val
{
   public int size { get; set; }
}

private void LoadQueue()
{
    // dont forget to replace the <TableName> with your SQLite table name
    List<Val> ints=db.Query<Val>("SELECT sum(Queue) as 'size' from <TableName> group by (strftime('%Y%m%d%H0', Interval)+strftime('%M',Interval)/5)");
    //now you can get the size for ints[0].size
    ...
    List<Queue> QList = new List<Queue>();

}

更新其他问题:

DateTime(QueueDate = DateTime.Now)有两种存储方式:

  1. 如果您使用new SQLite.Net.SQLiteConnection(new SQLitePlatformWinRT(), DBPath) 获取SQLiteConnection 并创建表。 您存储到 SQLite 中的 DateTime 是 Ticks 格式(例如 636071680313888433)。当你得到它时,它是 UTC 时间。

  2. 如果你想以yyyy-MM-dd HH:mm:ss的格式存储DateTime你需要使用new SQLiteConnection(new SQLitePlatformWinRT(), DBPATH,false);,其中false代表storeDateTimeAsTicks参数。默认为true。(如果您已经以第一种方式创建了表,则需要重新创建表并重新插入所有数据)。

QueueDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") 会将DateTime 存储为字符串。

更新 2: 如果您还想检索日期列,可以使用以下代码:

public class Val
{
   public DateTime Date { get; set; }
   public int Size { get; set; }
}
private void LoadQueue()
{
    List<Val> ints = db.Query<Val>("SELECT min(QueueDate) as 'Date',sum(Queue) as 'Size' from tblQueue group by (strftime('%Y%m%d%H0',QueueDate)+strftime('%M',QueueDate)/5) order by Queuedate");

}

【讨论】:

  • 夏:请看我的编辑。我有点困惑 tblQueue 类使用什么 DataType,Sqlite.Net-PCL 使用它来将 DateTime 插入 SQLite DB。请看我的 db.Insert -statement。谢谢
  • 我已经更新了我的答案,请检查一下。我认为您需要使用new SQLiteConnection(new SQLitePlatformWinRT(), DBPATH,false)
  • 夏:非常感谢您的帮助。 Select-statement 中的 Interval 是什么,它是指我的 tblQueue 中的 QueueDate?我将进行模拟队列数据收集并使用您的方法。我会回到这里分享我的发现。谢谢
  • @夏:请看我的Edit_2
  • 是的,间隔指的是QueueDate。我更新了我的答案,让 SQL 语句也检索QueueDate。请检查一下。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-04-23
  • 1970-01-01
  • 2018-01-30
  • 2019-07-05
  • 1970-01-01
  • 2013-08-12
  • 2019-05-21
相关资源
最近更新 更多