【问题标题】:How to get single row from Sqlite table from xamarin如何从 xamarin 的 Sqlite 表中获取单行
【发布时间】:2018-02-16 23:16:56
【问题描述】:

我正在 Xamarin.Android 中开发一个 Android 应用程序。我创建了一个 Sqlite 数据库,我可以在其中添加数据并获取所有数据。但现在我想从表中检索一个与列键对应的单行值。

这是我的AlbumTable代码:

namespace Myapp
{
  class AlbumTable
  {
    public long SR_ID { get; set; }   

    public string a{ get; set; }

    public string FileName { get; set; }

    public string OrderId { get; set; }

    public string Mobile { get; set; }

    public string Date { get; set; }

    public string CreatedOn { get; set; }

    public string UpdatedOn { get; set; }
  }
}

这是数据库中表的所有列名,我存储在数据库中。

这是 DatabaseHelper 文件代码:

namespace Myapp
{
  class DatabaseHelper
  {
    Java.IO.File dir = new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/Android/data/com.abhijit.testing.app/databases");

    public bool createDataBase()
    {
        try
        {
            using (var connection = new SQLiteConnection(Path.Combine(dir.AbsolutePath, "album.db")))
            {
                connection.CreateTable<AlbumTable>();
                return true;
            }
        }
        catch (SQLiteException e)
        {
            return false;
        }           
    }

    public bool InsertIntoTable(AlbumTable album)
    {
        try
        {
            System.Console.Write("Data Saved Successfully");

            using (var connection = new SQLiteConnection(Path.Combine(dir.AbsolutePath, "album.db")))
            {
                connection.Insert(album);
                return true;
            }
        }
        catch (SQLiteException e)
        {
            return false;
        }
    }

    public List<AlbumTable> getalldata()
    {
        try
        {
            using (var connection = new SQLiteConnection(Path.Combine(dir.AbsolutePath, "album.db")))
            {
                return connection.Table<AlbumTable>().ToList();
            }
        }
        catch (SQLiteException ex)
        {
            return null;
        }
    }
  }
}

现在我想根据orderid 字段从表中获取一条记录。 我想检索与orderid 匹配的一行。

我是 Xamarin.Android 的新手,所以我不知道该怎么做。

【问题讨论】:

    标签: c# sqlite xamarin.android


    【解决方案1】:

    1.您可以直接使用 FirstOrDefault() Linq 方法:-

    添加 using System.Linq; 命名空间。

    public AlbumTable GetRow(string orderId) {

    try
    {
        using (var connection = new 
       SQLiteConnection(Path.Combine(dir.AbsolutePath, "album.db")))
        {
    
            return connection.Table<AlbumTable>().FirstOrDefault(x => x.OrderId 
            == orderId);
        }
    }
    catch (SQLiteException ex)
    {
        return null;
    }
    

    }

    直接返回单条记录。

    1. 如果您将订单id作为主键,那么您也可以使用以下代码:- _connection.Find (id);

    【讨论】:

    • 如果您将订单id作为主键,那么您也可以使用以下代码:- _connection.Find (id);
    【解决方案2】:

    您只需使用与您的 getalldata 相同的代码并使用 Where(condition) 子句和 SingleOrDefault() 更改 .ToList() 即可检索 1 行或 null。

    public AlbumTable getRow(string orderId)
    {
    
        try
        {
            using (var connection = new SQLiteConnection(Path.Combine(dir.AbsolutePath, "album.db")))
            {
    
                return connection.Table<AlbumTable>().Where(x => x.OrderId == orderId).SingleOrDefault();
            }
    
        }
        catch (SQLiteException ex)
        {
            return null;
        }
    }
    

    【讨论】:

    • 这是否返回所有专辑字段
    • 不,只是表格的一行 --> SingleOrDefault() 表示 1 个唯一行或空
    • @unknownapk 不要忘记提供反馈:)
    【解决方案3】:

    如果我没记错的话,你正在寻找这样的东西:

    var getColumn=new DatabaseHelper().SelectTableDatafromQuery<AlbumTable>("SELECT YourColumnName FROM AlbumTable");
    

    此代码将获取上述变量中​​特定列的所有数据,祝你好运。

    要单独获取列数据,您可以使用 foreach 循环。

    【讨论】:

      猜你喜欢
      • 2016-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-14
      • 1970-01-01
      • 2021-11-30
      相关资源
      最近更新 更多