Sqlite Window Phone 8 中的更新记录 [英] Update Record in Sqlite Window Phone 8

查看:34
本文介绍了Sqlite Window Phone 8 中的更新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的数据库中,我有一个名为 question1 的表,初始默认状态分配为 = 0,然后每次我想更新时我都希望状态 = 1,我使用命令

In my database I have table named question1, with the initial default status assigned = 0, then every time I want to update I want the status = 1, I use the command

await conn.ExecuteAsync("UPDATE question1 SET Status = '1' WHERE id =" + ques1.id);

但它不起作用.谁知道这里出了什么问题?下面是读取数据和更新表的代码......

but it is not working. Who knows what is wrong here? Here's the code to read the data and update the table......

public async Task<question1> get_data_question1()
{
    SQLiteAsyncConnection conn = new SQLiteAsyncConnection("Database");
    var query = conn.Table<question1>().Where(x => x.Status == 0);
    var result = await query.ToListAsync();
    question1 ques1 = new question1();
    foreach (var item in result)
    {
        ques1.id = item.id;
        ques1.Status = item.Status;
    }
    // Update
    await conn.ExecuteAsync("UPDATE question1 SET Status = '1' WHERE id =" + ques1.id);
    return ques1;
}

推荐答案

这就是我设置简单数据库的方式.

This is how I would set up your simple database.

它将创建 3 个条目,所有状态 = 0.

It will create 3 entries with all Status = 0.

在代码中我说的那一行处设置一个断点;

Put a break point at the line where I said so in the code;

您会在该查询之后看到所有 0 值都已更新为 1.

You will see after that query all 0 values has been updated to 1.

using SQLite;
using System.IO;
using System.IO.IsolatedStorage;
using System.Threading.Tasks;

public class Question
{
    [SQLite.PrimaryKey, SQLite.AutoIncrement]
    public int Id { get; set; }
    public int Status { get; set; }
} 

public partial class MainPage : PhoneApplicationPage
{
    string dbPath = "";
    public MainPage()
    {
        InitializeComponent();
        dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "db.sqlite");                        

        CreateDBTable();
        InsertDB();         // only call this once (comment it out the next time you deploy)
        UpdateDB();

    }
}

// from the MSDN example
private async Task<bool> FileExists(string fileName)
{
    var result = false;
    try
    {
        var store = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
        result = true;
    }
    catch
    {
    }
    return result;
}

public void CreateDBTable()
{
    if (!FileExists("db.sqlite").Result)
    {
        using (var db = new SQLiteConnection(dbPath))
        {
            db.CreateTable<Question>();
        }
    } 
}

public void UpdateDB()
{
    using (var db = new SQLiteConnection(dbPath))
    {
        var existing = db.Query<Question>("SELECT * FROM Question WHERE Status = 0");
        if (existing != null)
        {
            foreach(Question q in existing)
            {
                db.Execute("UPDATE Question SET Status = 1 WHERE Id = ?", q.Id);
            }
        }


        // query again to see if has changed
        existing.Clear();

        existing = db.Query<Question>("SELECT * FROM Question WHERE Status = 1");
        if (existing != null)
        {
            int breakpoint_here = 1;
        }

    } 
}

private void InsertDB()
{
    using (var db = new SQLiteConnection(dbPath))
    {
        db.RunInTransaction(() =>
        {
            db.Insert(new Question() { Status = 0 });
            db.Insert(new Question() { Status = 0 });
            db.Insert(new Question() { Status = 0 });
        });
    }
}

这篇关于Sqlite Window Phone 8 中的更新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆