Common db api for crystal
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.
spec Fixes query_one? handling no rows (#86) Jul 10, 2018
src Release 0.5.1 Nov 7, 2018
.gitignore Update .gitignore to latest template Nov 7, 2018
.travis.yml enforce tool format in .travis.yml Sep 6, 2016 Release 0.5.1 Nov 7, 2018
LICENSE initial commit Feb 26, 2016 Add the Cassandra driver (#94) Nov 20, 2018
shard.yml Release 0.5.1 Nov 7, 2018

Build Status


Common db api for crystal. You will need to have a specific driver to access a database.


Add this to your application's shard.yml:

    github: crystal-lang/crystal-db



Since this is an abstract db api, it's usage is through a concrete database driver.

require "db"
require "sqlite3" "sqlite3:./file.db" do |db|
  # When using the pg driver, use $1, $2, etc. instead of ?
  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

  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 "#{} (#{})"
      # => Sarah (33)
      # => John Doe (30)


Issues not yet addressed

  • Support non prepared statements. #25
  • Time data type. (implementation details depends on actual drivers)
  • Data type extensibility. Allow each driver to extend the data types allowed.
  • Transactions & nested transactions. #27
  • Connection pool.
  • Direct access to IO to avoid memory allocation for blobs.


  1. Fork it ( )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request


  • bcardiff Brian J. Cardiff - creator, maintainer