【问题标题】:Xamarin Forms SQLiteXamarin 表单 SQLite
【发布时间】:2016-09-16 14:10:22
【问题描述】:

我想将我的数据保存在 SQLite 数据库中,所以我安装了 SQLite.Net-PLC nuget 并在 android 和 iOS 中创建了一个连接,然后我安装了 SQLite.Net.Async-PLC nuget 和创建了一个存储库,我在其中定义了函数:插入、更新和删除,例如:

public Task<int> InsertAsync(T entity) {
        return _connection.InsertAsync(entity);
    }

我也创建这样的表:

 public Repository(ISQLiteConnection connectionService) {
        _connectionService = connectionService;
        _connection = _connectionService.GetConnection();
        _connection.CreateTableAsync<T>();
    }

在我的页面中我想插入一个表格:

ISQLiteConnection connectionService = DependencyService.Get<ISQLiteConnection>();
var activityRepo = new Repository<Activity>(connectionService);
await activityRepo.InsertAsync(activity);

它给了我这个错误:SQLite.Net.SQLiteException: no such table: Activity

我放了一个断点,它正在创建表,并且在插入异步语句上弹出错误。

【问题讨论】:

  • 表也是异步创建的。你有没有尝试等待它?可能还没有创建表。
  • 我试图将创建表放在一个单独的函数中,然后使用 await 调用该函数,但它给了我这个错误:System.NotSupportedException:不了解 System.Collections.ObjectModel。 ObservableCollection`1[System.String]

标签: c# sqlite xamarin.forms


【解决方案1】:

您确实忽略了这行代码下的警告:

_connection.CreateTableAsync<T>();

警告将清楚地表明此调用未被等待。因此,Repository 的构造函数很可能在创建表之前返回。

var repo = new Repository(connectionService);
await repo.CreateTablesAsync();

并更改您的存储库:

public async Task CreateTablesAsync()
{
   await _connection.CreateTableAsync<T>();
}

请勿删除上述任何await 关键字,否则您将运行该方法而不是等待它结束。

【讨论】:

  • 感谢您的回复,它在 Task 和函数名称下给了我一个错误,因为它应该返回
  • @Mireille 答案已修复。请重新检查
  • 它给了我这个错误 System.NotSupportedException: Don't know about System.Collections.ObjectModel.ObservableCollection`1[System.String]
  • 好的,我知道了,我的 Activity 类中有一个可观察的集合,而 SQLite 不支持它
  • @Mireille 你不能在你的表中存储集合......你能举个例子来说明试图存储什么吗?
猜你喜欢
  • 2018-12-30
  • 2018-11-22
  • 2018-06-15
  • 1970-01-01
  • 2020-07-10
  • 1970-01-01
  • 2021-01-13
  • 2018-03-07
  • 1970-01-01
相关资源
最近更新 更多