can not find path for sqlite db in Maui app

amr yakout 40 Reputation points
2024-04-17T07:00:39.2033333+00:00

i try to create local sqlite db in maui app .

		private const string DbName = "SweetsShopDb.db3";
		private static string DbPath => Path.Combine(Environment.GetFolderPath(
										Environment.SpecialFolder.LocalApplicationData), DbName);
		public async Task Init()
		{
			Database = new SQLiteAsyncConnection(DbPath,
				SQLiteOpenFlags.Create | SQLiteOpenFlags.ReadWrite | SQLiteOpenFlags.SharedCache);
			await Database.CreateTableAsync<User>();
		}

after initializing the class

	builder.Services.AddSingleton<SweetsShopDb>();
    protected override async void OnAppearing() 	{ 		base.OnAppearing();
    
    		await _sweetsShopDb.Init(); 
}it gives me path that cannot accessed

"/data/user/0/com.amr.sweetsshop/files/SweetsShopDb.db3 so i cannot find this path ".

i checked for this path "C:\Users\HP\AppData\Local\Packages\com.amr.sweetsshop_9zz4h110yvjzm\LocalState"

but folder is empty

Developer technologies .NET .NET MAUI
Developer technologies Visual Studio Debugging
Developer technologies Visual Studio Testing
Developer technologies Visual Studio Other
Developer technologies C#
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,126 Reputation points Microsoft External Staff
    2024-04-18T07:50:38.5666667+00:00

    Hello,

    it gives me path that cannot accessed

    Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) this method is currently not available for mobile platforms in MAUI. For MAUI programs, you can use FileSystem to set the database address.

    #if ANDROID
            public static string DatabasePath =>
                Path.Combine(FileSystem.AppDataDirectory, DatabaseFilename);
    #elif WINDOWS
             public static string DatabasePath => Path.Combine(Environment.GetFolderPath(
                                            Environment.SpecialFolder.LocalApplicationData), DatabaseFilename);
    #endif
    

    but folder is empty

    The MAUI application on Windows will not release the file to the Local folder shown in the path, but will save it to C:\Users\username\AppData\Local\Packages\package_name\LocalCache\Local

    In addition, for the initialization of Sqlite, the officially recommended method is to apply lazy loading. You don't need to call the Init method manually, the database will automatically trigger initialization the first time you use one of the methods.

    The TodoItemDatabase uses asynchronous lazy initialization to delay initialization of the database until it's first accessed, with a simple Init method that gets called by each method in the class:

    For MAUI programs using Sqlite, you can refer to the following official documentation and examples for more information.

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    2 people found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.