2015-09-22 56 views
0

我正在製作UWP中的movieApp,如果用戶喜歡電影,我想存儲電影中的ID。我已經嘗試過使用txt,但存在拒絕訪問錯誤的問題。所以我決定使用sqlite保證。用SQLite插入新行PCL

我用它來獲取starded: https://code.msdn.microsoft.com/windowsapps/Implement-SQLite-Local-8b13a307#content

我一直在尋找,而現在,所有信息我找到sqlite的PCL dosent工作..它看起來像許多過dosent方法存在的例子我找到用途。

因此,似乎很少有例子在那裏..瑪貝你們可以把我在正確的道路..

這是到目前爲止我的代碼爲這個:

private void LikeIt() 
{ 
    var sqlpath = System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Filmdb.sqlite"); 

    using (SQLite.Net.SQLiteConnection conn = 
     new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), sqlpath)) 
    { 
     if (File.Exists(sqlpath)) 
     { 
      AdMovieID(); 
     } 
     else 
     { 
      conn.CreateTable<MovieID>(); 
      AdMovieID(); 
     } 


    } 
} 
private void AdMovieID() 
{ 
    var sqlpath = System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "Filmdb.sqlite"); 

    SQLite.Net.SQLiteConnection conn = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), sqlpath); 

    //This is where i have tried some of the things i have found 
    // like this: 
    //SQLite.Net.SQLiteCommand insertSQL = 
    //new SQLite.Net.SQLiteCommand("INSERT INTO MovieID(ID) VALUES(?), conn)<-- error: dosent work no constructor that takes arguments; 
    //insertSQL.Parameters.Add(MovieID.ID); 

    //Just to test if something is saved 
    var allaId = conn.Find<MovieID>(sqlpath).ID;//<-- just tried this and it dosent work either "Object reference not set to an instance of an object." 
    foreach (var id in allaId) 
    { 
     Test.Text = id.ToString(); 
    } 
} 

的類我使用SQLite使用:

public class MovieID 
    { 
     public string ID { get; set; } 
    } 

回答

1

你在任何地方插入一個新的項目在你的代碼,從我所看到的。

看看這個簡單的代碼示例:

using (var connection = new SQLiteConnection(path)) 
{ 
    connection.CreateTable<MovieID>(); 
    connection.Insert(new MovieID { ID = "someId" }); 
    var movies = connection.Table<MovieID>().ToList(); 
} 

如果你檢查到底movies它應該有一個項目裏面,有「someId」 ID的MovieId對象。

+0

非常感謝你現在的作品! 我已經嘗試了很多東西,他們有一些我有commentOut在上面的代碼..但就像我說它從來沒有工作.. 你是如何知道這個具體的命令?因爲我無法找到任何示例顯示這對於Sqlite PCL .. – Newbie1337