sqlite - 如何使用 Windows Azure 移动服务 SQLite 存储为 SQLite 表定义索引

标签 sqlite azure synchronization azure-mobile-services

我有一个 Windows Mobile 服务,其中包含一个包含 ProductID 和 ProductName 的表。我正在使用 azure 移动服务同步服务与云同步数据。但我找不到在客户端定义表索引的方法。

public class Product
{
    public string id;
    [JsonProperty(PropertyName = "ProductCode")]
    public int ProductCode{ get; set; }
    [JsonProperty(PropertyName = "ProductName")]
    public string ProductName { get; set; }
}

我将表格定义为

var store = new MobileServiceSQLiteStore("sqlite.db");
store.DefineTable<Product>();

现在如何在sqlite表中定义索引?我在 msdn 或任何其他论坛中找不到任何方法。

最佳答案

不幸的是,我没有找到任何解释这一点的文档。但我手动完成了它并且它对我有用。步骤如下:

  1. 首先安装此 Nuget 软件包 ( SQLite.Net PCL )。
  2. 创建 SQLite 表后,为所需的表和列创建索引。

代码示例

private async Task InitLocalStoreAsync()
{
    var store = new MobileServiceSQLiteStore("sqlite.db");
    store.DefineTable<Product>();
    await App.MobileService.SyncContext.InitializeAsync(store);

    // Create a new connection 
    using (var db = DbConnection)
    {
        //Adding index, say you want to add index for ProductCode column
        //Add index, table and column names respectively 
        db.CreateIndex("IX_Product_ProductCode", "Product", "ProductCode"); 
   }
}

private static SQLite.Net.SQLiteConnection DbConnection
{
   //Here I'm using UWP, if you are using other platforms
   //you have to change platform and database path.
   get
   {
       return new SQLite.Net.SQLiteConnection(
                new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(),
                System.IO.Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, "ipglocalstore.db"));
    }
}

如果您将 SQLite 与 UWP 结合使用,请查看 this article .

关于sqlite - 如何使用 Windows Azure 移动服务 SQLite 存储为 SQLite 表定义索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33466487/

相关文章:

Azure函数运行时异常,系统数据sqlclient异常的类型初始值设定项,无法加载DLL 'sni.dll'

node.js - 在数据库准备好之前运行 Jest 测试

python - 没有名为 _sqlite3 的模块

azure - 实现重试逻辑后显示角色分配已存在错误

java - volatile 关键字 : is the variable I am using among two threads synchronized?

netbeans - 比较 NetBeans 中的本地和远程文件

nhibernate - 将 NHibernate Session 与数据库同步 - 相反的方式

objective-c - Sqlite3步骤== SQLITE_ROW对我不起作用

Android SQLite修改后刷新 View 数据

Azure 文本翻译 API v3.0 - 可能的错误代码