0

I've already created an SQLite database with tables and inserted some records in it. I have it stored locally in some folder. The thing I'm trying to do is to add that SQLite file into my Xamarin project so I can read records out of it. I didn't use SQLite-net-pcl nugget package since I only need this database to read some records out of it from a single table for testing a framework on prototype app.

I've tried to follow this guide:Use a local database in Xamarin but I don't know how to add the database into the assets and I just can't find any other solution online.

I've tried adding the database file into assets but I get to this and don't know how to continue with import. Current assets (image)

Edit: I've managed to add the prototype.db file to the root of the Android project (hopefully correctly) and now I'm having issues with establishing connection to that database so I can perform queries. : Pic of the database position in project. I've also created the interface in the C# project and a class for implementing that interface in Android project. I'm not sure if I'm doing all the necessary steps that are required to connect the database and Xamarin project.

IDatabaseConnection Interface snippet:

using System;
using System.Collections.Generic;
using System.Text;
using SQLite;

namespace Qooharitsa_Prototype_App.Connections
{
    public interface IDatabaseConnection
    {
        SQLiteAsyncConnection Connection();
    }
}

DatabaseConnection Class snippet:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Qooharitsa_Prototype_App.Connections;
using Qooharitsa_Prototype_App.Droid;
using SQLite;
using Xamarin.Forms;

[assembly: Dependency(typeof(DatabaseConnection))]

namespace Qooharitsa_Prototype_App.Droid
{
    class DatabaseConnection : IDatabaseConnection
    {
        public SQLiteAsyncConnection Connection()
        {
            var documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments);
            var path = Path.Combine(documentsPath, "prototype.db");

            return new SQLiteAsyncConnection(path);
        }
    }
}

How do I check if the connection to the database has been established?

  • you linked to a very detailed step-by-step tutorial - what specifically are you stuck on? Why aren't you using the nuget? Even if you don't need all the functionality of the package it's still the easiest way to work with sqlite in a Xamarin project. – Jason Nov 09 '21 at 14:05
  • I'm having hard times understanding the whole nugget package. So basically I made a database with a single table and some records in it. I am trying to establish a connection between that database and my project. Currently I am on step 4 where I have to manually copy DB out of APK, but I don't know where this posted Code snippet should go. Should it go into the C# or Android project, and do I put it into a class or something. I am really lost at the moment. – Dorian Setnik Nov 09 '21 at 14:09
  • I am not sure even if I'm doing the right thing with connecting this database with project. Is there any better way to do it as I'm currently beginner in android development and this database option seemed best to me at the moment. – Dorian Setnik Nov 09 '21 at 14:10
  • do you need to write to the db, or is it read-only? And does this need to work in iOS and Android, or only one of them? – Jason Nov 09 '21 at 14:10
  • Only reading from database and only Android. – Dorian Setnik Nov 09 '21 at 14:11
  • if it's read-only you don't need to copy it. You should be able to place it in the root of your project as build-type "Content" and then just open it directly from that path – Jason Nov 09 '21 at 14:18
  • Do I still need to add the connection to that database with some code, or can I just access that data now? – Dorian Setnik Nov 09 '21 at 14:29
  • yes, you will need to use an `SQLiteConnection` to open the db – Jason Nov 09 '21 at 14:33
  • Okay, all understood, thank You for Your help Sir! – Dorian Setnik Nov 09 '21 at 14:34
  • Hm, I'm still having issues with establishing that connection, could You please specify a bit more on how to connect to the database? – Dorian Setnik Nov 09 '21 at 20:42
  • please edit your question to show what specifically you are doing and what specific error or issue you're having – Jason Nov 09 '21 at 20:43

0 Answers0