简体   繁体   中英

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.

I have a database 'mydb' with columns like this:

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. 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.

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.

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?

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"; – Rob Peterson 2 hours ago

This comment worked flawlessly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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