How can I use Sqlite in a .NET Standard library?

6.6k views Asked by At

Does anyone know how to setup sqlite in a .NET Standard library? SQLite.Net-PCL doesn't seem to be compatible, or at least that's what nuget tells me.

2

There are 2 answers

2
Christian Findlay On BEST ANSWER

Yep, this is a .NET Standard implementation of an SQLite wrapper:

https://github.com/MelbourneDeveloper/SQLite.Net.Standard.git

Install-Package SQLite.Net.Standard

It's very simple and there is only one DLL which works on all platforms. It also targets PCL.

0
user16012721 On

Here is the full sample. Pure .Net Standard

Nuget Package => sqlite-net-pcl

https://www.nuget.org/packages/sqlite-net-pcl/1.7.335?_src=template

Nuget Package => System.Data.SQLite https://www.nuget.org/packages/System.Data.SQLite/1.0.114?_src=template

Or the always updated sqlite from microsoft

https://www.nuget.org/packages/Microsoft.Data.Sqlite

using DB.Extensions;
using DB.Modellers;
using SQLite;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace DB
{
    public class DatabaseAdapterStandard
    {
        
        public string SqlFilePath { get; }
        public DatabaseAdapterStandard(string sqlFilePath)
        {
            this.SqlFilePath = sqlFilePath;
          
        }
       
      
        public IEnumerable<books> GetBooks()
        {
            using (var session = new SQLiteConnection(SqlFilePath))
            {
                var books = session.Table<books>().ToList();
                return books;
            }
        }
        public IEnumerable<chapters> GetChapters(books books)
        {
            using (var session = new SQLiteConnection(SqlFilePath))
            {
                var items = session.Table<chapters>().Where(p=>p.reference_human == books.human).ToList();
                return items;
            }
        }
        public IEnumerable<verses> GetVerses()
        {
            using (var session = new SQLiteConnection(SqlFilePath))
            {
                var items = session.Table<verses>().ToList();
                return items;
            }
        }
    }
}