Forum Xamarin.Forms
We are excited to announce that the Xamarin Forums are moving to the new Microsoft Q&A experience. Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

We encourage you to head over to Microsoft Q&A for .NET for posting new questions and get involved today.

Sqlite.Net db file not creating

jagdeeshkjagdeeshk Member ✭✭

In my project am using sqlite its working fine in debug mode when coming to release mode its not working. In release mode db file not creating.

    public static void GetDBFile(string dbPath, ISQLitePlatform sqlitePlatform)
    {
        if (dbUtils == null)
        {
            dbUtils = new DataAccess(sqlitePlatform, dbPath);
        }
    }

I written like this in App.xaml.cs file calling this method in Mainactivity.cs ( Android project )

        dbPath = FileAccessHelper.GetLocalFilePath("dbfilename.sqlite");
    App.GetDBFile(dbPath, new SQLitePlatformAndroidN());

Best Answer

Answers

  • ShantimohanElchuriShantimohanElchuri USMember ✭✭✭✭✭

    @jagdeeshk Can you give the code of the constructor of the class DataAccess?

  • jagdeeshkjagdeeshk Member ✭✭

    DataAccess> @ShantimohanElchuri said:

    @jagdeeshk Can you give the code of the constructor of the class DataAccess?

    DataAccess.cs

        private SQLiteAsyncConnection dbConn;
    
        public DataAccess(ISQLitePlatform sqlitePlatform, string dbPath)
        {
            try
            {
                if (dbConn == null)
                {
                    var connectionWithLock = new SQLiteConnectionWithLock(sqlitePlatform, new SQLiteConnectionString(dbPath, storeDateTimeAsTicks: false));
    
                    dbConn = new SQLiteAsyncConnection(()=>connectionWithLock);
                    dbConn.CreateTableAsync<TableOne>();
                    dbConn.CreateTableAsync<TableTwo>();
    
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
    
  • ShantimohanElchuriShantimohanElchuri USMember ✭✭✭✭✭
    edited April 26

    @jagdeeshk It seems you are working on a old PCL project, is it not? If so probably I will not be able help you much. I am using current Xamarin Forms templates and in this the code is much simpler as below:

            public MyDbOps(string dbPath)
            {
                dbConn = new SQLiteAsyncConnection(dbPath);
                dbConn.CreateTableAsync<ToDoPlusItem>().Wait();
            }
    

    I am using NuGet package "sqlite-net-pcl" by Frank A. Krueger. Long time back I moved out of PCL projects. In the current setup, I tried to use the following line from your code:

    var connectionWithLock = new SQLiteConnectionWithLock(sqlitePlatform, new SQLiteConnectionString(dbPath, storeDateTimeAsTicks: false));
    

    But it is flagging an error that the SQLiteConnectionWithLock doesn't have a constructor that takes two arguments. So the following code is valid here.

    var conn = new SQLiteConnectionWithLock(new SQLiteConnectionString(dbPath, false));
    

    Since it worked fine in Debug but not working in Release mode, now you have to check if you have set any of the project properties in Debug mode to make it work specifically. Many of the properties are to be set separately for Debug and Release modes.

    Since you are using connection string, are you connecting to an external server and not to an embedded SQLite database?

  • jagdeeshkjagdeeshk Member ✭✭
    Accepted Answer

    @ShantimohanElchuri Thankyou.
    Just added Mono.Data.sqlite at linker ignore assemblies. It's resolved my error.

  • ShantimohanElchuriShantimohanElchuri USMember ✭✭✭✭✭

    @jagdeeshk said:
    @ShantimohanElchuri Thankyou.
    Just added Mono.Data.sqlite at linker ignore assemblies. It's resolved my error.

    That's for Android. Did you do the same for iOS too?

    Mark your post as Answer for the benefit of others.

  • jagdeeshkjagdeeshk Member ✭✭

    Present I am working on only android part, I have no idea about iOS.

Sign In or Register to comment.