SQLite in the PCL project

SpaceMonkeySpaceMonkey ✭✭✭GBMember ✭✭✭

Can I add the SQLite.NET component to my cross-platform Xamarin.Forms project?
I don't see any option to add components, I can only do that for the native Android/iOS project.

Best Answer

Answers

  • kentuckerkentucker ✭✭✭✭✭ USMember ✭✭✭✭✭
    Accepted Answer

    There is a PCL sqlite library you can use.

    https://www.nuget.org/packages/SQLite.Net-PCL/

  • SpaceMonkeySpaceMonkey ✭✭✭ GBMember ✭✭✭

    @ken_tucker actually, I've just found out that the SQLite.NET component suggested by Xamarin does have a PCL package on NuGet. Thanks a lot

  • SpaceMonkeySpaceMonkey ✭✭✭ GBMember ✭✭✭

    @ken_tucker nevermind, the one I posted doesn't work, I will try what you suggested.

  • SpaceMonkeySpaceMonkey ✭✭✭ GBMember ✭✭✭
    edited May 2015

    I'm an idiot. There is a PCL version:
    https://www.nuget.org/packages/sqlite-net-pcl/

    Edit: Which doesn't seem to be working:
    System.Exception: Something went wrong in the build configuration. This is the bait assembly, which is for referencing by portable libraries, and should never end up part of the app. Reference the appropriate platform assembly instead.

  • ScottMacRitchieScottMacRitchie CAMember

    @SpaceMonkey Have you been able to resolve the exception? I'm getting the same thing. From what I understand it's because the platform specific sqlite implementation isn't being loaded, but I haven't been able to figure out how to fix that.

  • SpaceMonkeySpaceMonkey ✭✭✭ GBMember ✭✭✭
    edited July 2015

    @ScottMacRitchie

    I don't even remember why I said what I said in the previous comment. Anyway:

    Add SQLite-Net-PCL to the PCL project and the native project too.

    In your PCL when you need to initialise a new connection you'll need an ISQLitePlatform implementation. You do that using dependency injection:

    So in the PCL you're doing:

    ISQLitePlatform platform = Utils.NativeMethods.Value.GetSqlite ();  
    

    and in the native project (Android in my case) you're doing:

            public ISQLitePlatform GetSqlite() {
                return new SQLitePlatformAndroid ();
            }
    
  • ScottMacRitchieScottMacRitchie CAMember

    Thanks @SpaceMonkey

  • ScottNimrodScottNimrod ✭✭ USMember ✭✭
    edited July 2015

    I receive an SQLite exception when creating a connection.

    This worked before I installed VS2015 RTM.

    Client (PCL):

     _databaseConnection = DependencyService.Get<IDatabase>().Connect();
    

    Android project:

     public SQLiteConnection Connect()
     {
        var fileName = "my_file.db3";
        var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
        var path = Path.Combine(documentsPath, fileName);
        var connection = new SQLiteConnection(path);
    
        return connection;
     }
    

    I receive an exception when executing:

     var connection = new SQLiteConnection(path);
    

    Something went wrong in the build configuration. This is the bait
    assembly, which is for referencing by portable libraries, and should
    never end up part of the app.

    NOTE:

    SQLitePCL.raw_basic is on 0.7.1
    I get errors whenever I attempt to upgrade the version to 0.8.1

    Again, this all worked before I installed VS2015 RTM
    Any suggestions?

  • SpaceMonkeySpaceMonkey ✭✭✭ GBMember ✭✭✭

    @ScottNimrod

    Are you using this:

    https://www.nuget.org/packages/SQLite.Net-PCL/

    The way I explained in the previous comment?

  • ScottNimrodScottNimrod ✭✭ USMember ✭✭

    @SpaceMonkey
    I followed your steps. But I am confused.
    I just don't know what to do with the platform object.

    var platform = DependencyService.Get().GetSqlite();

    [PublicAPIAttribute]
    public interface ISQLitePlatform
    {
        IReflectionService ReflectionService { get; }
        ISQLiteApi SQLiteApi { get; }
        IStopwatchFactory StopwatchFactory { get; }
        IVolatileService VolatileService { get; }
    }
    

    It's not apparent to me on how I am to perform any CRUD operation with this platform interface.
    Any suggestions?

  • SpaceMonkeySpaceMonkey ✭✭✭ GBMember ✭✭✭

    @ScottNimrod

    Creating a connection requires an object implementing ISQLitePlatform. You should pass the object you get using DependecyService

  • YkshLeoYkshLeo ✭✭✭✭ USMember ✭✭✭✭

    Refer the below link to get working sample about SQLite.NET :
    C-Sharp Corner : Interacting With Local Database in Xamarin.Forms

  • jeffchen.5589jeffchen.5589 USMember

    after installation SQLite-net-pcl, I donot see SQLite.net namespace, but only see sqlit namespace
    why?

    thanks

  • AlejandroSeguraAlejandroSegura ✭✭ USMember ✭✭

    jeffchen.5589 Did you figure out where SQLite.net is?

  • AlejandroSeguraAlejandroSegura ✭✭ USMember ✭✭

    For me the fix was to uninstall the package SQLite-net-pcl and install SQLite-Net-PCL. Notice the difference in punctuation.

    I did this in PCL and platform projects.

Sign In or Register to comment.