简体   繁体   English

使用ADO.NET(Xamarin,C#)从简单的SQLite数据库查询给定值的最新条目

[英]Querying the latest entry of a given value from a simple SQLite database with ADO.NET (Xamarin, C#)

I think this is a simple one for those with experience in SQLite, but it's pretty opaque to me. 我认为对于那些有SQLite经验的人来说,这是一个简单的方法,但是对我来说却是不透明的。

I have a database 'mydb' with columns like this: 我有一个像这样的列的数据库“ mydb”:

Name Date Val1 Val 2 Val3
Greg time1 blah blah blah
Stev time2 blah blah blah

My android app allows users to select their name from a profile, and when they make changes to value based on the app, it adds a column. 我的Android应用程序允许用户从配置文件中选择他们的姓名,并且当他们基于该应用程序更改值时,它会添加一列。 An update may look like this: 更新可能如下所示:

Name Date Val1 Val 2 Val3
Greg time1 blah blah blah
Stev time2 blah blah blah
Stev time3 blah1 blah1 blah1
Stev time4 blah2 blah2 blah2
Greg time5 blah1 blah1 blah1

In a portion of my code, I want to load up the values of the latest entry for a given name. 在我的代码的一部分中,我想为给定名称加载最新条目的值。 In my above example, I'd want Greg at time5, or if I selected Stev, I want Stev and time4. 在上面的示例中,我想要在时间5的Greg,或者如果我选择Stev,那么我想要Stev和time4。

I can sort of do this with the following command where 'Name' is a string and the name the users chosen to load from the database: 我可以使用以下命令来完成此操作,其中“名称”是一个字符串,用户选择从数据库中加载该名称:

  contents.CommandText = "SELECT * FROM mydb WHERE [Name] = @Name AND [Date] = (Select MAX([Date]) from mydb)"; 
  contents.Parameters.AddWithValue("@Name", Name);

Of course, this only allows me to select the latest set of values period. 当然,这只允许我选择一组最新的值。 I cannot access Stev's latest data as an example, because the above code only allows me to select the most recent. 我无法访问Stev的最新数据作为示例,因为上面的代码仅允许我选择最新的数据。

I was intending to get the latest entryof the user whose name I put input. 我打算获取输入了姓名的用户的最新条目。 Can someone with ADO.NET experience help me craft the appropriate command? 有ADO.NET经验的人可以帮助我编写适当的命令吗?

I think what you want is to add ORDER BY DATE DESC LIMIT 1 so "SELECT * FROM mydb WHERE [Name] = @Name ORDER BY DATE DESC LIMIT 1"; 我认为您要添加的是ORDER BY DATE DESC LIMIT 1,因此“ SELECT * FROM mydb WHERE [Name] = @Name ORDER BY DATE DESC LIMIT 1”; – Rob Peterson 2 hours ago –罗伯·彼得森2小时前

This comment worked flawlessly. 此评论完美无瑕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM