Use SQLITE ODBC with .NET on MAC OS

0

Hi I'm trying to do a very simple program in .NET (3.1 Core or 5) something like this:

using System;
using System.Data.Odbc;

namespace testsqlite
{
    class Program
    {
        static void Main(string[] args)
        {
        var conn = new OdbcConnection();
            conn.ConnectionString = "Driver=SQLITE3;Database=Orders.db;LongNames=0;Timeout=1000;NoTXN=0;SyncPragma=NORMAL;StepAPI=0;";
            conn.Open();
            var stmt = conn.CreateCommand();
            stmt.CommandText = "Select * from customer";
            using (var reader = stmt.ExecuteReader()) 
            {
               var message = reader.GetString(0);
               Console.WriteLine(message);
            }
            Console.WriteLine("Hello World!");
        }
    }
}

I installed sqlite3 and also installed the driver using brew install sqliteodbc but it does now seem to work. When I run I get:Unhandled exception. System.Data.Odbc.OdbcException (0x80131937): ERROR [01000] [unixODBC][Driver Manager]Can't open lib 'SQLITE3' : file not found

Any ideas of what I might be missing?

macos
sqlite
.net-core
asked on Stack Overflow Mar 29, 2021 by orellabac

1 Answer

0

Ok I found a way to make it work.

  1. Before you start make sure that you have sqlite installed. If not run brew install sqlite.

  2. Second install the sqlite odbc driver by running brew install sqliteodbc

NOTE: you might need to do brew install upgrade sqliteodbc

  1. After installing the sqliteodbc driver, you need to run odbcinst -j. You do this to validate your unixODBC installation because you need to know where is the odbcinst.ini file in your system. ODBSINST output

For example in my case it was /usr/local/etc/odbcinst.ini

  1. Before you edit the ini file you need to know where is the odbc library for sqlite installed. run brew test sqliteodbc getting odbc lib location

  2. Finally modify your odbcinst.ini to include a location for the odbc driver odbcinst with driver

  3. After that you can just use your sqlite odbc connection string. In my case it was something like this: DRIVER=SQLITE3;Database=Orders.db; LongNames=0; Timeout=1000; NoTXN=0; SyncPragma=NORMAL; StepAPI=0;

Sometimes I have found errors when leaving spaces between Database= the database name and the last ; just make sure you dont because if not the driver wont find the .db file

answered on Stack Overflow Mar 31, 2021 by orellabac

User contributions licensed under CC BY-SA 3.0