如何从SQLite基于间隔获取数据

时间:2016-08-15 13:55:51

标签: sqlite winrt-xaml uwp uwp-xaml

说,我有以下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分钟获取一次总队列?

示例
第1分钟,队列总数为:130
第2分钟,队列总数为:80左右

第3分钟?

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; }     

------- Edit_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 show:150,70对于y轴

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

这些值(2.1&amp; 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)");  

}

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

由于

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

感谢。

1 个答案:

答案 0 :(得分:0)

  

如何使用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>();

}

更新其他问题:

有两种方法可以存储DateTimeQueueDate = DateTime.Now):

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

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

  3. new SQLiteConnection(new SQLitePlatformWinRT(), DBPATH,false);会将storeDateTimeAsTicks存储为字符串。

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

    QueueDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")