0

I have created an SQLite Database in .NET MAUI

public static class Constants
{
    private const string DBFileName = "SQLite.db3";
    public const SQLiteOpenFlags Flags = 
        SQLiteOpenFlags.ReadWrite |
        SQLiteOpenFlags.Create |
        SQLiteOpenFlags.SharedCache;
    public static string DatabasePath
    {
        get
        {
            return Path.
               Combine(FileSystem.Current.AppDataDirectory, DBFileName);
        }
    }
}

The application is already running. But I want to define an absolute path so that I could easily get the db3 file. The reason is that I need to view the data graphically and manage it using DB Browser for SQLite. The problem is that when I change it to an absolute path, I am getting an error.

This is the error I encounter:

SQLite.SQLiteException
  Message=Could not open database file: C:\Users\H00062147\.android\avd\pixel_7_-_api_35.avd\data/SQLite.db3 (CannotOpen)
  Source=SQLite-net
  StackTrace:
   at SQLite.SQLiteConnection..ctor(SQLiteConnectionString connectionString)
   at SQLite.SQLiteConnection..ctor(String databasePath, SQLiteOpenFlags openFlags, Boolean storeDateTimeAsTicks)
   at IMS_Android.Repositories.TransactionRepository..ctor() in C:\Sulmaca Folder\IMS_Android\IMS_Android\IMS_Android\Repositories\TransactionRepository.cs:line 20
   at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Constructor(Object obj, IntPtr* args)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=9.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].VisitCallSiteMain(ServiceCallSite callSite, RuntimeResolverContext argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=9.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].VisitCallSite(ServiceCallSite callSite, RuntimeResolverContext argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=9.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].VisitCallSiteMain(ServiceCallSite callSite, RuntimeResolverContext argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=9.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=9.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].VisitCallSite(ServiceCallSite callSite, RuntimeResolverContext argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(ServiceIdentifier serviceIdentifier)
   at System.Collections.Concurrent.ConcurrentDictionary`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceIdentifier, Microsoft.Extensions.DependencyInjection, Version=9.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[Microsoft.Extensions.DependencyInjection.ServiceProvider.ServiceAccessor, Microsoft.Extensions.DependencyInjection, Version=9.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].GetOrAdd(ServiceIdentifier key, Func`2 valueFactory)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(ServiceIdentifier serviceIdentifier, ServiceProviderEngineScope serviceProviderEngineScope)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
   at Microsoft.Maui.MauiContext.WrappedServiceProvider.GetService(Type serviceType) in /_/src/Core/src/MauiContext.cs:line 72
   at Microsoft.Maui.MauiContext.WrappedServiceProvider.GetService(Type serviceType) in /_/src/Core/src/MauiContext.cs:line 72
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[IApplication](IServiceProvider provider)
   at Microsoft.Maui.MauiApplication.OnCreate() in /_/src/Core/src/Platform/Android/MauiApplication.cs:line 46
   at Android.App.Application.n_OnCreate(IntPtr jnienv, IntPtr native__this) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net9.0/android-35/mcw/Android.App.Application.cs:line 1056
   at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V(_JniMarshal_PP_V callback, IntPtr jnienv, IntPtr klazz) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:line 22

What I have tried is that I add

private const string DBDesiredPath = "``C:\\Users\\H00062147\\.android\\avd\\pixel_7_-_api_35.avd\\data``";

(Currently, I am using an android emulator)

And instead of using "FileSystem.Current.AppDataDirectory", I used this DBDesiredPath.

What I did is return Path.Combine(DBDesiredPath, DBFileName); And this is where an error is displayed.

How can I properly define an absolute path for SQLite db3 file so I can easily get it?

6
  • I don't think that you can use an absolute path from the host machine for this. Any paths outside of the app data directory are usually protected and require special permissions. In this particular case you also have to note that your emulator runs in virtual environment that does not have any knowledge about the host machine it's running on. It cannot open any paths from your development machine. Commented May 22 at 8:00
  • @Julian Thanks, I got your point, do you have any recommendation how can I set a path inside the android device where I could manually get the db3 file? Is it possible? Commented May 22 at 8:10
  • Instead of FileSystem.Current.AppDataDirectory have you tried Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)? Commented May 22 at 11:53
  • you need to use adb to extract files from the Android filesystem Commented May 22 at 12:43
  • @newbie6381741 As Jason mentioned, you can use the Android Debug Bridge (adb) to access files on the Android device's file system from Windows. However, that requires that the file is not in the private application data folder of the app. You'll need to request permissions from the Android OS to write to and read from external storage. Then, you will be able to find the file via some file explorer or access it via adb. Commented May 22 at 12:56

0

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.