2017-09-23 83 views
0

大家好我已經在xamarin中實現了sqlite數據庫,我需要搜索任何specfic數據存在或不在數據庫中。如何檢查數據是否存在或不在xamarin數據庫

我要檢查,如果數據或行是有或沒有在數據庫根據專輯密鑰

這裏是我的數據庫類

namespace FacebookAuth 
{ 
    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; 
      } 
     } 

     public List<AlbumTable> SelectAlbum(string orderid) 
     { 
      try 
      { 
       using (var connection = new SQLiteConnection(Path.Combine(dir.AbsolutePath, "album.db"))) 
       { 
        return connection.Query<AlbumTable>("SELECT * From album Where AlbumKey=?", orderid);//not working exception raised that invalid table name 
       } 
      } 
      catch (SQLiteException ex) 
      { 
       return null; 
      } 
     } 

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

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

和數據庫中的列名如下

K 
class AlbumTable 
{ 
    [PrimaryKey] 
    public string Id { get; set; } 

    public string ZipFillPath { get; set; } 

    public string CoverPhotoPath { get; set; } 

    public string AlbumKey { get; set; } 

    public string NoOfPages { get; set; } 

    public string Email { get; set; } 

    public string LastName { get; set; } 

    public string FirstName { get; set; } 

    public string City { get; set; } 

    public string Address1 { get; set; } 

    public string ZipPostalCode { get; set; } 

    public string PhoneNumber { get; set; } 
} 

如何檢查值出現在DB或不

+0

作爲第一步,您應該爲類DatabaseHelper添加一個新方法getAlbum(字符串albumKey)。 returntype必須是AlbumTable。 – JRB

回答

0
  1. 開放Android Device MonitorTools->Android->Android Device Monitor enter image description here
  2. 導航到你的數據庫文件和數據庫文件複製出來到您的PC。 enter image description here
  3. 您可以使用任何SQLite瀏覽器打開數據庫來檢查您的數據是否在數據庫中。例如http://sqlitebrowser.org/
+0

我想這樣做有問題sir –

0

docs非常適合提供易於遵循的說明。

你需要你的id列是一個int,那麼如果你想使用SQL(見下一點,爲什麼你可能想)你可以使用Get<T>()方法

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

,你需要用定義的名字創建表格,否則給表格的名字可能是一些編程創建的非人性化的內部亂碼。

[Table("Albums")] 
class AlbumTable 
{ 
    [PrimaryKey] 
    public int Id { get; set; } 
... 

如果你要不停的id列作爲字符串(也就是你必須在數據庫中沒有控制),你可以使用LINQ獲得

return connection.Get<AlbumTable>().FirstOrDefault(a => a.Id == orderid); 

注意,這加載所有的數據到內存等,如果你在數據庫中獲得大量數據以及丟失數據庫的內在優點 - 存儲和搜索數據就是它們的目的,那麼性能就會很差。這不是實體框架,因此對於複雜的查詢,請使用上面的直接Sql語句。

+0

not working.i沒有得到你的觀點 –

+0

在'AlbumTable'類中,更改'public string Id {get;組; }'到'public int Id {get;組; }'並使用'return connection.Get (orderid);'(其中'orderid'是一個'int')。 –

相關問題