无法在SQLiteAsyncConnection中转换数据库路径

时间:2017-03-19 23:49:51

标签: c# .net sqlite xamarin

我正在学习如何使用SQLite.Net.Async在移动设备上创建本地数据库。我正在逐步学习教程,但我的数据库路径有问题。错误消息显示'参数1无法从字符串转换为System.Func SQLite.Net.SQLiteConnectionWithLock'。我很困惑,因为这段代码在教程样本上运行良好。

这是我的代码:

using SQLite.Net.Async;

namespace ToDoList
{
    public class TodoItemDatabase
    {
        readonly SQLiteAsyncConnection asyncdatabase;

        public TodoItemDatabase (string dbPath)
        {
            asyncdatabase = new SQLiteAsyncConnection(dbPath);
            //I haven't done further because I was stucked by dbPath error
        }
    }
}

示例代码效果很好:

using System.Collections.Generic;
using System.Threading.Tasks;
using SQLite;

namespace Todo
{
public class TodoItemDatabase
{
    readonly SQLiteAsyncConnection database;

    public TodoItemDatabase(string dbPath)
    {
        database = new SQLiteAsyncConnection(dbPath);
        database.CreateTableAsync<TodoItem>().Wait();
    }

    public Task<List<TodoItem>> GetItemsAsync()
    {
        return database.Table<TodoItem>().ToListAsync();
    }

    public Task<List<TodoItem>> GetItemsNotDoneAsync()
    {
        return database.QueryAsync<TodoItem>("SELECT * FROM [TodoItem] WHERE [Done] = 0");
    }

    public Task<TodoItem> GetItemAsync(int id)
    {
        return database.Table<TodoItem>().Where(i => i.ID == id).FirstOrDefaultAsync();
    }

    public Task<int> SaveItemAsync(TodoItem item)
    {
        if (item.ID != 0)
        {
            return database.UpdateAsync(item);
        }
        else {
            return database.InsertAsync(item);
        }
    }

    public Task<int> DeleteItemAsync(TodoItem item)
    {
        return database.DeleteAsync(item);
    }
}
}

1 个答案:

答案 0 :(得分:0)

这是示例应用程序sqlite-net-pcl

中使用的包