問題描述
在我的數(shù)據(jù)庫中,我有一個名為 question1 的表,初始默認狀態(tài)分配為 = 0,然后每次我想更新時我都希望狀態(tài) = 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);
但它不起作用.誰知道這里出了什么問題?下面是讀取數(shù)據(jù)和更新表的代碼......
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;
}
推薦答案
這就是我設置簡單數(shù)據(jù)庫的方式.
This is how I would set up your simple database.
它將創(chuàng)建 3 個條目,所有狀態(tài) = 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 });
});
}
}
這篇關(guān)于Sqlite Window Phone 8 中的更新記錄的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!