2014-10-20 60 views
0

有我的问题;在windows phone 8.1中使用SQLite c#

我需要在我wp8.1应用程序使用SQLite,我已经安装nugets,扩展等 我有一个JSON字符串保存到我的分贝。

我知道我必须创建一个类,我将用它来创建我的sqlite表。

接下来,我知道我必须为表格创建fct,插入fct以将我的Json字符串放入我的表格中,并通过读取将其返回并使用它。

我找到教程等,每次我尝试的东西,它不起作用。

我不是一个亲在C#和Windows Phone 8.1中,我不赞成在数据库中。自从三天以来,我在一面大墙前。

有人可以帮我吗?

谢谢你很多。

+0

你试过了什么?你遇到过任何错误吗? – Kulasangar 2014-10-21 02:56:06

+0

@Kulasangar我把代码放在一个新的答案。 Thx你 – 2014-10-21 10:24:10

+0

我看不到任何答案! – Kulasangar 2014-10-21 10:35:29

回答

0

我为我的表创建了一个Json类;

下面是代码:

namespace PhoneApp5.Model 
{ 
    public class Json 
    { 
      [PrimaryKey] 
      public string Key { get; set; } //Key for my table 
      public string String { get; set; } //value String Json for my table 
    } 
} 

然后,我创造了一些fontion使用表:

private async void CreateTable() // create my table 
{ 
    SQLiteAsyncConnection conn = new SQLiteAsyncConnection("blog"); 
    await conn.CreateTableAsync<Json>(); 
} 

public async void InsertJson(Json json) // insert my Json into my table 
{ 
    SQLiteAsyncConnection conn = new SQLiteAsyncConnection("blog"); 
    await conn.InsertAsync(json); 
} 

public async Task<Json> GetJson(string Key) // read my Json string from table 
{ 
    SQLiteAsyncConnection conn = new SQLiteAsyncConnection("blog"); 

    var query = conn.Table<Json>().Where(x => x.Key == Key); 
    var result = await query.ToListAsync(); 

    return result; // result here is not working because of convertion of System.Connection.Generic.List<PhoneApp5.Model.Json> into PhoneApp5.Model.Json 
} 

public async void UpdatePost(Json json) // fct update 
{ 
    SQLiteAsyncConnection conn = new SQLiteAsyncConnection("blog"); 
    await conn.UpdateAsync(json); 
} 

public async void DeletePost(Json json) // fct delete 
{ 
    SQLiteAsyncConnection conn = new SQLiteAsyncConnection("blog"); 
    await conn.DeleteAsync(json); 
} 

然后,我不知道该怎么办。

你需要帮忙

+0

您正在为您的项目使用哪个库?哪个版本? – 2014-10-27 11:54:41