• Stars
    star
    139
  • Rank 262,954 (Top 6 %)
  • Language
    Crystal
  • License
    MIT License
  • Created over 9 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

SQLite3 bindings for Crystal

crystal-sqlite3 Build Status

SQLite3 bindings for Crystal.

Check crystal-db for general db driver documentation. crystal-sqlite3 driver is registered under sqlite3:// uri.

Installation

Add this to your application's shard.yml:

dependencies:
  sqlite3:
    github: crystal-lang/crystal-sqlite3

Usage

require "sqlite3"

DB.open "sqlite3://./data.db" do |db|
  db.exec "create table contacts (name text, age integer)"
  db.exec "insert into contacts values (?, ?)", "John Doe", 30

  args = [] of DB::Any
  args << "Sarah"
  args << 33
  db.exec "insert into contacts values (?, ?)", args: args

  puts "max age:"
  puts db.scalar "select max(age) from contacts" # => 33

  puts "contacts:"
  db.query "select name, age from contacts order by age desc" do |rs|
    puts "#{rs.column_name(0)} (#{rs.column_name(1)})"
    # => name (age)
    rs.each do
      puts "#{rs.read(String)} (#{rs.read(Int32)})"
      # => Sarah (33)
      # => John Doe (30)
    end
  end
end

DB::Any

  • Time is implemented as TEXT column using SQLite3::DATE_FORMAT_SUBSECOND format (or SQLite3::DATE_FORMAT_SECOND if the text does not contain a dot).
  • Bool is implemented as INT column mapping 0/1 values.

Setting PRAGMAs

You can adjust certain SQLite3 PRAGMAs automatically when the connection is created by using the query parameters:

require "sqlite3"

DB.open "sqlite3://./data.db?journal_mode=wal&synchronous=normal" do |db|
  # this database now uses WAL journal and normal synchronous mode
  # (defaults were `delete` and `full`, respectively)
end

The following is the list of supported options:

Name Connection key
Busy Timeout busy_timeout
Cache Size cache_size
Foreign Keys foreign_keys
Journal Mode journal_mode
Synchronous synchronous
WAL autocheckoint wal_autocheckpoint

Please note there values passed using these connection keys are passed directly to SQLite3 without check or evaluation. Using incorrect values result in no error by the library.

More Repositories

1

crystal

The Crystal Programming Language
Crystal
19,438
star
2

shards

Dependency manager for the Crystal language
Crystal
764
star
3

crystal-book

Crystal reference with language specification, manuals and learning materials
Makefile
395
star
4

crystal-db

Common db api for crystal
Crystal
303
star
5

crystal_lib

Automatic binding generator for native libraries in Crystal
Crystal
138
star
6

heroku-buildpack-crystal

Heroku buildpack for Crystal
Shell
123
star
7

crystal-mysql

MySQL connector for Crystal
Crystal
107
star
8

install-crystal

GitHub Action: Install Crystal programming language
JavaScript
67
star
9

html_builder

DSL for creating HTML
Crystal
67
star
10

crystal-website

crystal-lang.org website
SCSS
61
star
11

clang.cr

libclang bindings for crystal (including automatic C bindings generator)
Crystal
47
star
12

distribution-scripts

Shell
40
star
13

crystal-presents

Playground for creating crystal presentations with live coding
JavaScript
40
star
14

crystal-molinillo

A generic dependency resolution algorithm. Ported from https://github.com/CocoaPods/Molinillo/
Crystal
30
star
15

perf-tools

An assortment of tools to track resources in Crystal applications
Crystal
26
star
16

omnibus-crystal

Omnibus builder for Crystal
Ruby
25
star
17

bootstrap-script

Automated script to bootstrap the crystal compiler from source
Shell
22
star
18

crystal-readline

Crystal bindings to GNU Readline Library
Crystal
21
star
19

json_mapping.cr

Crystal
18
star
20

rfcs

RFCs repository for Crystal
18
star
21

crystal-env

Crystal
13
star
22

homebrew-crystal

Homebrew Tap for Crystal development
Ruby
13
star
23

logger.cr

Crystal
12
star
24

crystal-dist

Docker workspace to sign and release Crystal binaries to repositories
Shell
7
star
25

yaml_mapping.cr

Crystal
7
star
26

test-ecosystem

Shell
7
star
27

crystal-infrastructure

Infrastructure automation for Crystal related resources
Jinja
3
star
28

crystal-random

Crystal
3
star
29

crystal-intro

Talk that introduces the Crystal programming language
JavaScript
2
star
30

osc-docker

Dockerfile for pushing to Open Build Service
Dockerfile
1
star
31

.github

Default community health files
1
star