导入windows phone后无法更新SQLite DB中的值

时间:2014-04-04 04:20:39

标签: c# sqlite windows-phone-7 windows-phone-8

我按照本教程导入现有数据库http://wp.qmatteoq.com/import-an-already-existing-sqlite-database-in-a-windows-8-application/将数据库复制到项目文件并将构建操作设置为内容后,我将数据库复制到隔离存储。现在我尝试更新数据库中的值数据库,并在我的绑定列表框中查看更改,我无法看到更改。当我使用独立存储资源管理器并查看数据库时,DB中的值已更改。但是查询它不会返回更新的值。我是在两种情况下使用相同的连接字符串。

//copy DB from installation folder to isolated storage
private async Task CopyDatabase()
{
    bool isDatabaseExisting = false;

    try
    {
        StorageFile storageFile = await ApplicationData.Current.LocalFolder.GetFileAsync("sample.db");
        isDatabaseExisting = true;
    }
    catch
    {
        isDatabaseExisting = false;
    }

    if (!isDatabaseExisting)
    {
        StorageFile databaseFile = await Package.Current.InstalledLocation.GetFileAsync("sample.db");
        await databaseFile.CopyAsync(ApplicationData.Current.LocalFolder);
    }
}

//update the value
 dbConn = new SQLiteConnection(DB_PATH);
               dbConn.Query<resources>(" update resources SET isFavorite = 'True' WHERE resourceId =" + rid);
           }
// Query database to populate list box
     string DB_PATH = Path.Combine(ApplicationData.Current.LocalFolder.Path, "sample.db");
     private SQLiteConnection dbConn;
     dbConn = new SQLiteConnection(DB_PATH);
     List<resources> retrievedpdf = dbConn.Query<resources>("select * from resources where categoryId=" + noid + " AND typeId=1" ).ToList<resources>();
      pdflist.ItemsSource = retrievedpdf;

enter image description here

我可以在资源管理器中查看更改,但查询会返回未修改的值。

2 个答案:

答案 0 :(得分:1)

我希望查询中的一些更改对我有很大帮助。

//Copy your database file From Installation folder to IsolatedStorage on App launching.

//Change your update query bu below query
int updateCount = dbConn.Execute("update resources SET isFavorite = 'True' WHERE resourceId =" + rid);

//Change your select Query
 **Edits**

List<resources> retrievedpdf = dbConn.Table<resources>().ToList().Where(x=>x.categoryId.Equals(noid) && x.typeId.Equals(1)).ToList();

//Assign ListBox ItemSource to null before assign actual source

pdflist.ItemsSource = null; 
pdflist.ItemsSource = retrievedpdf;

答案 1 :(得分:0)

查询出现问题,我一直在更新字符串&#34; true&#34;表格中的bool。这是正确的查询int updateCount = dbConn.Execute("update resources SET isFavorite = 1,modifiedTime = DATETIME('now') WHERE resourceId =" + rid);