实现SQLite Xamarin.Forms - 无法创建SQLiteAsyncConnection

时间:2018-02-13 12:05:32

标签: c# sqlite xamarin.forms

我正试图在本文中将SQLite实现到我的Xamarin.Forms共享资源项目中。

https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/databases/

我的新手眼睛似乎都设置正确,但是在创建数据库连接时似乎存在问题。它说它无法隐式转换类型SQLite.SQLiteAsyncConnection到TechsportiseApp.Data.TechsportiseData

我无法解释为什么会这样。有什么想法吗?

using System;
using System.Collections.Generic;
using System.Text;
using SQLite;
using Xamarin.Forms;
using static TechsportiseApp.Helpers.GlobalFunctions;
using TechsportiseApp.Models;
using System.Threading.Tasks;

namespace TechsportiseApp.Data
{
    class TechsportiseData
    {

        public TechsportiseData(string dbPath)
        {
            database = new SQLite.SQLiteAsyncConnection(dbPath);
            database.CreateTableAsync<Scan>().Wait();
            database.CreateTableAsync<Timing>().Wait();
        }

        static TechsportiseData database;

        public static TechsportiseData Database
        {
            get
            {
                if (database == null)
                {
                    database = new TechsportiseData(DependencyService.Get<IFileHelper>().GetLocalFilePath("TechsportiseData.db3"));
                }
                return database;
            }
        }

        public Task<List<Timing>> GetTimingsAsync()
        {
            return database.Table<Timing>().ToListAsync();
        }

        public Task<List<Timing>> GetTimingsNotUploadedAsync()
        {
            return database.QueryAsync<Timing>("SELECT * FROM [Timing] WHERE [Uploaded] = 0");
        }

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

        public Task<int> SaveTimingAsync(Timing timing)
        {
            if (timing.ID != 0)
            {
                return database.UpdateAsync(timing);
            }
            else
            {
                return database.InsertAsync(timing);
            }
        }

        public Task<int> DeleteTimingAsync(Timing timing)
        {
            return database.DeleteAsync(timing);
        }

        public Task<int> DeleteAllTimingsAsync()
        {
            return database.DeleteAllAsync(timing);
        }

        public Task<List<Scan>> GetScansAsync()
        {
            return database.Table<Scan>().ToListAsync();
        }

        public Task<List<Scan>> GetScansNotUploadedAsync()
        {
            return database.QueryAsync<Timing>("SELECT * FROM [Scan] WHERE [Uploaded] = 0");
        }

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

        public Task<int> SaveScanAsync(Scan scan)
        {
            if (scan.ID != 0)
            {
                return database.UpdateAsync(scan);
            }
            else
            {
                return database.InsertAsync(scan);
            }
        }

        public Task<int> DeleteScanAsync(Scan scan)
        {
            return database.DeleteAsync(scan);
        }

        public Task<int> DeleteAllScanssAsync()
        {
            return database.DeleteAllAsync(scan);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

第一个也是最明显的事情是没有意义

This compilation unit is not on build path of java project.

将其更改为此

static TechsportiseData database;

答案 1 :(得分:0)

所以事实证明代码部分

static TechsportiseData database;

public static TechsportiseData Database
{
    get
    {
        if (database == null)
        {
            database = new TechsportiseData(DependencyService.Get<IFileHelper>().GetLocalFilePath("TechsportiseData.db3"));
        }
        return database;
    }
}

需要在App.cs中,我没有意识到。现在好看!