【问题标题】:Max number from database数据库中的最大数量
【发布时间】: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


【解决方案1】:

您可以尝试按降序进行选择(强制转换为整数)并仅获得第一个结果:

select * from person ORDER BY CAST(score AS INTEGER) Desc limit 1;

【讨论】:

  • 我试一试 Ptit Xav,这是做什么的Score &lt; 2 ORDER BY CAST
  • 编辑了我之前的评论:使用限制只得到一行
  • 感谢 Ptit Xav,接缝工作。但我认为最好将 Column 更改为 Decimal 以进行进一步计算。
【解决方案2】:

谢谢@Jason

使用它就可以了

公开十进制分数 { get;放; } // 在类中

Score = Decimal.Parse(resultText.Text), // 保存

【讨论】:

    猜你喜欢
    • 2012-05-26
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-24
    • 1970-01-01
    • 2016-08-12
    • 1970-01-01
    相关资源
    最近更新 更多