Accessing Databases

Crystal can work with a growing number[50] of SQL and NoSQL databases. Among them are SQLite, MySQL (or MariaDB), Postgres, MongoDB,[51] Redis,[52] and ReThinkDB.[53]

The Crystal team realized the importance of database access for a programming language, so they made a DB module in a package called crystal-db,[54] which provides a common unified database API. Crystal-db works seamlessly together with the SQLite, MySQL, and Postgres driver. You don’t need to explicitly require it. For other databases, you will need to add specific drivers.

Let’s illustrate the basic database operations using the SQLite sample database chinook.db. This database represents a music shop and contains tables artists and albums among others. Because ...

Get Programming Crystal now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.