Skip to content

Basic Sqlite.Ecto Tutorial

Jason M Barnes edited this page May 20, 2015 · 26 revisions

Introduction

Sqlite.Ecto is an Ecto adapter which helps you to interact with SQLite databases.

This very brief tutorial will walk you through the basics of configuring and using Ecto with SQLite. We are going to setup a very basic schema that one might need for a blog. The following assumes you already have some familiarity with Elixir development.

PLEASE NOTE that the following schema is in no way secure and should not be used for a production database. It is only being used to demonstrate some features of Ecto.

Configuring Ecto

Let's create our new Elixir code with mix: mix new blog. Change into the new directory and update the mix.exs file to use Ecto and SQLite:

def application do
  [applications: [:logger, :sqlite_ecto, :ecto]]
end

defp deps do
  [{:sqlite_ecto, "~> 0.0.2"}]
end

Now make sure you can download your dependencies, compile, and setup your Ecto repository:

$ mix deps.get
$ mix ecto.gen.repo Blog.Repo

Edit the Blog.Repo module in lib/blog/repo.ex to use the Sqlite.Ecto adapter:

defmodule Blog.Repo do
  use Ecto.Repo, otp_app: :blog, adapter: Sqlite.Ecto
end

And change the default PostgreSQL configuration in config/config.exs to the following:

config :blog, Blog.Repo,
  adapter: Sqlite.Ecto,
  database: "blog.sqlite3",
  # pool options:
  size: 1,
  max_overflow: 0

In this example blog.sqlite3 is the SQLite file that will store our blog's database. The file will be created in the top-level directory. You can change it to any file path you like. Note the last two options define the number of worker processes that will connect to our database file. These options are recommended for testing purposes. While SQLite supports concurrent access to database files, not enough testing has been done with Sqlite.Ecto at this time to verify that concurrent access will not corrupt the database file.

Clone this wiki locally