Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sqlite3_open_v2 broken (Unity3D) #324

Closed
MeikelLP opened this issue Jan 8, 2020 · 5 comments · Fixed by #336
Closed

sqlite3_open_v2 broken (Unity3D) #324

MeikelLP opened this issue Jan 8, 2020 · 5 comments · Fixed by #336

Comments

@MeikelLP
Copy link

MeikelLP commented Jan 8, 2020

I'm using Microsoft.Data.Sqlite to access a sqlite3 file from my Unity3D game.

so I tried like:

using (var conn = new SqliteConnection($"Data Source=Assets\\Data\\database.bwxsdb"))
{
    conn.Open();
}

and I get:

NullReferenceException: Object reference not set to an instance of an object
SQLitePCL.raw.sqlite3_open_v2 (SQLitePCL.utf8z filename, SQLitePCL.sqlite3& db, System.Int32 flags, SQLitePCL.utf8z vfs) (at <15ecb38d58394d7b88b3f841a7dda078>:0)
SQLitePCL.raw.sqlite3_open_v2 (System.String filename, SQLitePCL.sqlite3& db, System.Int32 flags, System.String vfs) (at <15ecb38d58394d7b88b3f841a7dda078>:0)
Microsoft.Data.Sqlite.SqliteConnection.Open () (at <9ffe4c48f3134a7b905b5da527410f26>:0)

I tried this:

var fromString = utf8z.FromString("Assets\\Data\\database.bwxsdb");
var utf8Z = utf8z.FromString(null);
raw.sqlite3_open_v2(fromString, out var db, 0, utf8Z);

and get the same error.

Any reasons why that is not working? The db file is not broken. I can read & write to it via DataGrip.

@MeikelLP MeikelLP changed the title Open broken (Unity3D) sqlite3_open_v2 broken (Unity3D) Jan 8, 2020
@MeikelLP
Copy link
Author

MeikelLP commented Jan 8, 2020

Apparently I forgot to add required libraries. As we are in the Unity world and cannot use tools like NuGet I have to add the libraries all by myself. So what I need to work with in Unity are:

  • Microsoft.Data.Sqlite.dll
  • SQLitePCLRaw.batteries_v2.dll
  • SQLitePCLRaw.core.dll
  • SQLitePCLRaw.provider.e_sqlite3.dll
  • System.Buffers.dll
  • System.Memory.dll
  • System.Runtime.CompilerServices.Unsafe.dll

... and native libraries depending on your platform from the SQLitePCLRaw.lib.e_sqlite3 NuGet package.

Is there a better way to do this?

@bricelam
Copy link
Contributor

bricelam commented Mar 3, 2020

v1 used to provide a better exception message:

You need to call SQLitePCL.raw.SetProvider(). If you are using a bundle package, this is done by calling SQLitePCL.Batteries.Init().

@bricelam
Copy link
Contributor

bricelam commented Mar 3, 2020

@MeikelLP Have you seen NuGetForUnity? I'm not sure if it supports everything you need to install SQLitePCLRaw, but it's worth a shot.

@MeikelLP
Copy link
Author

MeikelLP commented Mar 3, 2020

@bricelam Yes I've seen this but I prefer OpenUPM. But there is no package yet available.

@MeikelLP
Copy link
Author

Today I noticed this error again. Not in Unity but rather in ASP.NET Core 3.1.3.
This issue occurs in a console app which I use to wrap my ef migrations add and ef database update without booting up my actual asp net app all the time. Very basic but works fine:

public class Program
{
    public static void Main(string[] args)
        => CreateHostBuilder(args).Build().Run();

    public static IHostBuilder CreateHostBuilder(string[] args)
    {
        return Host.CreateDefaultBuilder(args)
            .ConfigureServices(x => x.AddDbContext<NucleusDbContext>(options =>
            {
                var configuration = x.BuildServiceProvider().GetRequiredService<IConfiguration>();
                options.UseSqlite(configuration.GetConnectionString("DefaultConnection"));
            }));
    }
}

The problem is that I get the exact same error as I got before with Unity:

PS F:\projekte\none\MyCoolProject\Backend\Core> dotnet ef database update --startup-project ..\Migrator\
Build started...
Build succeeded.
info: Microsoft.EntityFrameworkCore.Infrastructure[10403]
      Entity Framework Core 3.1.3 initialized 'NucleusDbContext' using provider 'Microsoft.EntityFrameworkCore.Sqlite' with options: using lazy-loading proxies
System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.Data.Sqlite.SqliteConnection.Open()
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnection(Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)
   at Microsoft.EntityFrameworkCore.Sqlite.Storage.Internal.SqliteDatabaseCreator.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.HistoryRepository.Exists()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabaseImpl(String targetMigration, String contextType)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
Object reference not set to an instance of an object.

This can be easily fixed with adding the package reference to that migrator project as well:

<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="3.1.3" />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants