0

I am trying to implement SQLite into my Xamarin.Forms Shared Assets project from this article.

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

All appears to be set up correctly tyo my novice eyes, however there seems to be a problem in creating the database connection. It says its Not able to implicitly convert type SQLite.SQLiteAsyncConnection to TechsportiseApp.Data.TechsportiseData

I can't work out why this is. Any ideas?

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);
        }
    }
}
1
  • 2
    Hrm my Spidey senses you didn't follow the tutorial completely and thoroughly Commented Feb 13, 2018 at 12:09

2 Answers 2

1

The first and most obvious thing is this doesn't make sense

static TechsportiseData database;

change it to this

readonly SQLiteAsyncConnection database;
Sign up to request clarification or add additional context in comments.

Comments

0

So it turns out the section of code

static TechsportiseData database;

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

Needed to be in App.cs which I didn't realize. Seems good now!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.