5

I am trying to implement a SQLite database using c# and microsoft.data.sqlite in a .Net Standard 2.0 Project.

I keep getting the error "Method not found: 'IntPtr SQLitePCL.sqlite3.get_ptr()'" when I call connection.Open()

I have downloaded the following Nuget Packages:

<ItemGroup>
    <PackageReference Include="Microsoft.Data.SQLite" Version="2.2.6" />
    <PackageReference Include="Microsoft.Data.Sqlite.Core" Version="2.2.6" />
    <PackageReference Include="SQLitePCLRaw.bundle_green" Version="2.0.0" />
    <PackageReference Include="SQLitePCLRaw.core" Version="2.0.0" />
    <PackageReference Include="SQLitePCLRaw.lib.e_sqlite3" Version="2.0.0" />
    <PackageReference Include="SQLitePCLRaw.provider.e_sqlite3" Version="2.0.0" />
    <PackageReference Include="SQLitePCLRaw.provider.e_sqlite3.netstandard11" Version="1.1.14" />
  </ItemGroup>

I am executing it as follows:

private static SqliteConnection _sqlConnection = new SqliteConnection(@"Data Source = myPath");

public static Int64 InsertTransaction(string stringToInsert)
        {
            SQLitePCL.Batteries_V2.Init();
            StringBuilder sqlToExecute = new StringBuilder();
            sqlToExecute.Append("INSERT INTO table");
            sqlToExecute.Append("(columnName) ");
            sqlToExecute.Append("VALUES(' " + stringToInsert+ "')");

            using (SqliteConnection connection = _sqlConnection)
            {
                connection.Open();
                SqliteTransaction transaction = connection.BeginTransaction();
                SqliteCommand sqlCommand = new SqliteCommand(sqlToExecute.ToString(), connection, transaction);
                sqlCommand.ExecuteNonQuery();
            }
        }

Everything builds. At runtime, when the following block executes, I get: "Method not found: 'IntPtr SQLitePCL.sqlite3.get_ptr()'"on Connection.Open();

  • This won't help with your current issue, but I'd suggest reading up on https://stackoverflow.com/questions/14376473/what-are-good-ways-to-prevent-sql-injection . – mjwills Aug 22 '19 at 01:21
  • Also with your current code I'd suggest not using `StringBuilder` - it isn't buying you much value (since the vast majority of the strings are hard-coded). – mjwills Aug 22 '19 at 01:21
  • @mjwills - That's always a fine link to share... I'm actually familiar with SQL Injection attacks... I was simplifying a bit to keep the example concise. Similarly, I am using StringBuilder as I am actually looping through parameters... – David Mitchell Aug 22 '19 at 07:29
  • I'm having the exact same issue and it's driving me insane. This whole thing worked perfectly up until a few days. WTH happened?! – Crono Sep 03 '19 at 19:47

1 Answers1

4

Update of Microsoft.Data.Sqlite and Microsoft.Data.Sqlite.Core packages to Entity Framework Core version 3.0.0 will solve the issue. See my similar question asked on Github. And here are my package.config references:

<packages>
  <package id="Microsoft.Data.Sqlite" version="3.0.0-preview9.19423.6" targetFramework="net461" />
  <package id="Microsoft.Data.Sqlite.Core" version="3.0.0-preview9.19423.6" targetFramework="net461" />
  <package id="SQLitePCLRaw.bundle_e_sqlite3" version="2.0.2-pre20190904113843" targetFramework="net461" />
  <package id="SQLitePCLRaw.bundle_green" version="2.0.2-pre20190904113843" targetFramework="net461" />
  <package id="SQLitePCLRaw.core" version="2.0.2-pre20190904113843" targetFramework="net461" />
  <package id="SQLitePCLRaw.lib.e_sqlite3" version="2.0.2-pre20190904113843" targetFramework="net461" />
  <package id="SQLitePCLRaw.provider.dynamic_cdecl" version="2.0.2-pre20190904113843" targetFramework="net461" />
  <package id="System.Buffers" version="4.5.0" targetFramework="net462" />
  <package id="System.Memory" version="4.5.3" targetFramework="net462" />
  <package id="System.Numerics.Vectors" version="4.6.0-preview5.19224.8" targetFramework="net461" />
  <package id="System.Runtime.CompilerServices.Unsafe" version="4.6.0-preview9.19421.4" targetFramework="net461" />
</packages>
zimny
  • 136
  • 1
  • 8