【发布时间】:2021-06-03 12:26:50
【问题描述】:
我在查找数据库中的最大数量时遇到了一些麻烦。 这里也有问题 https://docs.microsoft.com/en-us/answers/questions/295657/max-number-from-sql.html 具有名称 Person 和列 Score 的表 我想在Score中找到最高分
public class Person
{
[PrimaryKey, AutoIncrement]
public int PersonID { get; set; }
public string Name { get; set; }
public string RollNo { get; set; }
public string G1 { get; set; }
public string G2 { get; set; }
public string G3 { get; set; }
public string G4 { get; set; }
public string G5 { get; set; }
public string G6 { get; set; }
public string G7 { get; set; }
public string G8 { get; set; }
public string G9 { get; set; }
public string G10 { get; set; }
public string G11 { get; set; }
public string G12 { get; set; }
public string G13 { get; set; }
public string G14 { get; set; }
public string G15 { get; set; }
public string G16 { get; set; }
public string G17 { get; set; }
public string G18 { get; set; }
public string Score { get; set; }
}
还有这个
public Task<List<Person>> GetItemsHighscore()
{
return db.QueryAsync < Person >("select * from [Person] where [Score]=(select max([Score]) from [Person])");
}
并显示在 CollectionView 中
async void Button_Clicked(object sender, EventArgs e)
{
lstPersons3.ItemsSource = await App.SQLiteDb.GetItemsHighscore();
}
但它显示的是基于第一个数字的最高数字,例如 4 高于 11
我认为问题可能出在字符串上?
也试过这个,但没有帮助。 没有错误,但结果是一样的
我需要做什么或改变什么?
public decimal Score { get; set; } // in Class
Score = (decimal)double.Parse(resultText.Text), // Save
resultText.Text = person.Score.ToString(); // Show
在 HIGH 的图像中必须有 11 而不是 4
【问题讨论】:
-
你永远不应该将数值作为字符串存储在你的数据库中——这是你的根本问题
-
现在我知道,经过 2 天的麻烦,我该如何更改数据库以存储数值
-
SQLiteAsyncConnection 数据库;公共 SQLiteHelper(string dbPath) { db = new SQLiteAsyncConnection(dbPath); db.CreateTableAsync
().Wait(); db.CreateTableAsync ().Wait(); } -
更改表定义以使用十进制而不是字符串
-
这是我已经尝试过的,但结果是一样的 // 将其更改为十进制 public decimal Score { get;放; } // 保存 Score = (decimal)double.Parse(resultText.Text), // 加载 resultText.Text = person.Score.ToString(); // 在 CollectionView 中显示 Label Text="{Binding Score}"
标签: sqlite xamarin.forms