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

Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
4,614 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,886 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,268 questions
Visual Studio Debugging
Visual Studio Debugging
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Debugging: The act or process of detecting, locating, and correcting logical or syntactical errors in a program or malfunctions in hardware. In hardware contexts, the term troubleshoot is the term more frequently used, especially if the problem is major.
941 questions
Visual Studio Testing
Visual Studio Testing
Visual Studio: A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.Testing: The act or process of applying tests as a means of analysis or diagnosis.
329 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 35,576 Reputation points Microsoft Vendor
    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.

    0 comments No comments