2017-09-08 14 views
0

我正在開發中Xamarin.Android Android應用程序得到SQLite表單列。我創建了一個SQLite數據庫,我可以添加數據,並得到所有數據。但現在我想要從一個表中的一行值,相應的列項。如何從xamarin

這裏是我的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的新手,所以我不知道如何去做。

回答

0

您只需要使用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; 
    } 
} 
+0

是否填報的專輯領域 –

+0

否,該表的只有一行 - >的SingleOrDefault()是指1個特有的行或空 – hugoterelle

+0

@unknownapk不要忘了給反饋:) – hugoterelle

0

如果我沒看錯你正在尋找的東西是這樣的:

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

這個代碼將獲得上述變量好運特定列的所有數據。

並單獨獲取列數據可以使用foreach循環。

0

1.You可以直接使用FirstOrDefault()LINQ的方法用於此: -

使用System.Linq的添加;命名空間。

公共AlbumTable GETROW(字符串的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);
+0

如果您的訂單ID作爲主鍵,然後就可以用同樣下面的代碼: - _connection.Find (ID); –