更改数据库

时间:2015-10-19 10:08:26

标签: c# sqlite blob

我在sqlite数据库文件中有一个BLOB列,我想更改此字段以提供更多信息。

BLOB包含以下文字:

  

“{\” 号码\ “:\” 35 \”,\ “通道类型\”:\ “TV \”,\ “服务名称\”:\ “DVBLink \”,\ “ImageInfos \”:[], \“IsInMixedFolder \”:false,\“Name \”:\“13th Street HD \”,\“Id \”:\“466ab1fb9be4f77e65ad2b229a15c326 \”,\“DateCreated \”:\“2015-09-14T12:55: 51.4081476Z \ “\ ”DateModified \“:\ ”0001-01-01T00:00:00.0000000Z \“,\ ”DateLastSaved \“:\ ”2015-10-17T13:34:37.4214116Z \“,\” LockedFields \ “:[],\” 的ParentId \ “:\” 00000000000000000000000000000000 \”,\ “工作室\”:[],\ “流派\”:[],\ “ProviderIds \”:{\ “ProviderExternalId \”:\ “13250000 \”}}“

这里是获取此信息的代码

        private void button1_Click(object sender, EventArgs e)
    {
        try {
            SQLiteConnection con = new SQLiteConnection("Data Source=D:\\2work\\VisualStudio\\Projects\\MapTVIcons\\library.db");

            con.Open();

            SQLiteCommand cmd = new SQLiteCommand();

            cmd.Connection = con;
            cmd.CommandText = "SELECT guid,data FROM TypedBaseItems where type = 'MediaBrowser.Controller.LiveTv.LiveTvChannel'";
            //cmd.CommandText = "SELECT * FROM TypedBaseItems";

            DataSet ds = new DataSet();
            SQLiteDataAdapter da = new SQLiteDataAdapter(cmd);
            ds.Tables.Add("TypedBaseItems");
            da.Fill(ds);
            //dataGridView1.DataSource = ds.Tables[1];
           foreach (DataRow dr in ds.Tables[1].Rows)
            {


                byte[] array = (byte[])dr["data"];
                var str = System.Text.Encoding.Default.GetString(array);
            }
        }
        catch(Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }
}

我想要做的是读出Id值(在blob Id \“:”466ab1fb9be4f77e65ad2b229a15c326内)并添加“ImageInfos”的路径并将新BLOB放回数据库文件。

1 个答案:

答案 0 :(得分:1)

这样做的一个不那么短的方法是扩展你已经尝试过的东西:

  1. 在C#对象中读取json blob(在项目中包含Newtonsoft.Json nuget包)

        var json = "JSON STRING";
    
        var ro = JsonConvert.DeserializeObject<RootObject>(json);
    
  2. 上面将转换为以下C#类

    public class ProviderIds
    {
        public string ProviderExternalId { get; set; }
    }
    
    
    public class RootObject
    {
    public string Number { get; set; }
    
    public string ChannelType { get; set; }
    public string ServiceName { get; set; }
    public List<object> ImageInfos { get; set; }
    public bool IsInMixedFolder { get; set; }
    public string Name { get; set; }
    public string Id { get; set; }
    public string DateCreated { get; set; }
    public string DateModified { get; set; }
    public string DateLastSaved { get; set; }
    public List<object> LockedFields { get; set; }
    public string ParentId { get; set; }
    public List<object> Studios { get; set; }
    public List<object> Genres { get; set; }
    public ProviderIds ProviderIds { get; set; }
    }
    
  3. 然后您可以简单地操纵您喜欢的字段。

  4. 并将其更新回数据库