【问题标题】:Insert new row with SQLite PCL使用 SQLite PCL 插入新行
【发布时间】:2015-12-20 01:02:53
【问题描述】:

我正在 UWP 中制作电影应用程序,如果用户喜欢电影,我想存储电影的 id。我已经尝试过使用 txt 但访问被拒绝错误有很多问题。所以我决定使用 sqlite。

我用它来获得stard: https://code.msdn.microsoft.com/windowsapps/Implement-SQLite-Local-8b13a307#content

我已经搜索了一段时间,我发现的所有信息都可以与 sqlite pcl 一起使用。看起来我找到的示例使用的许多方法都存在。

所以看来那里的例子很少..也许你们可以以正确的方式推动我..

这是我到目前为止的代码:

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; }
    }

【问题讨论】:

    标签: c# linq sqlite uwp


    【解决方案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 对象。

    【讨论】:

    • 非常感谢它现在可以工作了!我已经尝试了很多东西我在上面的代码中有commentOut..但就像我说它从来没有工作过..你怎么知道这个具体的命令?因为我找不到任何可以为 Sqlite PCL 显示这一点的示例..
    猜你喜欢
    • 2016-12-28
    • 1970-01-01
    • 2014-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多