0
votes

I am using an Android tablet with a Xamarin/C# application. With this tablet I am trying to access an SQLite database that resides on my Windows 10 machine. I opted to use the USB/Android vs the Android emulator, and the application still resides on the Windows 10 machine. It seems irrelevant, but I've also tried mapping a drive, sharing the folder, and creating a network connection. Project References

This is the code:

using Microsoft.Data.Sqlite;
using System;
using System.Windows.Input;
using Xamarin.Essentials;
using Xamarin.Forms;

namespace CicoAndroid { public partial class MainPage : ContentPage { public ICommand TapCommand => new Command(async (url) => await Launcher.OpenAsync(url)); private readonly string db1 = @"Data Source=\LAPTOP-FJUIC9RR\db\db.sqlite"; public MainPage() { InitializeComponent(); }

    private void LoginClicked(object sender, EventArgs e)
    {
        int rows;
        try
        {
            Console.WriteLine("DB1: " + db1);
            SqliteConnection conn = new SqliteConnection(db1);
            conn.Open();  ***This is the line that is throwing the exception.***
            using (SqliteCommand cmd = conn.CreateCommand())
            {
                cmd.CommandText = "SELECT COUNT(*) FROM [tblUsers] WHERE USERNAME = 'someUser'";
                rows = (int)cmd.ExecuteScalar();
                Console.WriteLine("ROWS: " + rows);
                cmd.Dispose();
            }
            conn.Dispose();
        }
        catch (Exception ex)
        {
           lblAuth.Text = "FUL: a.c.us" + Environment.NewLine + ex.ToString();
           return;
        }

I am very very new to Xamarin and C#, and I've tried tens of variations with the code and NuGet SQLite packages. Any help will be greatly appreciated.

Thank you

1
so your app will only be usable if you are on the same local network as the desktop containing the database?Jason

1 Answers

0
votes

Xamarin.Forms supports database-driven applications using the SQLite database engine, which makes it possible to load and save objects in shared code. Article Xamarin.Forms Local Databases describes how Xamarin.Forms applications can read and write data to a local SQLite database using SQLite.Net.

Integrate SQLite.NET into mobile apps by following these steps:

  1. Install the NuGet package.

  2. Configure constants.

  3. Create a database access class.

  4. Access data in Xamarin.Forms.

  5. Advanced configuration.

For more details, you can check document:

https://docs.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/data/databases .

And there is also a sample included in above links, you can check and refer to it's code.

Besides, you can also check document Store data in a local SQLite.NET database.