/duckdb-nim

Nim SQL Driver For DuckDB - A fast analytical database system

Primary LanguageNim

Nim SQL Driver for DuckDB database engine
A fast analytical database system

nimble install duckdb

API reference
Github Actions Github Actions

Note

The high-level API is still a work in progress. Please check back later for updates.

😍 Key Features

  • Low-level API for direct access to DuckDB
  • High-level API for easy database management
  • Supports SQL queries, transactions, and prepared statements
  • Supports reading and writing data in various formats (CSV, JSON, Parquet)
  • Supports DuckDB's in-memory and persistent storage modes
  • Cross-platform compatibility (Linux, macOS, Windows)
  • Easy to use with Nim's powerful type system and macros

Examples

import duckdb

var db = open("my_database.duckdb")
var dbConn = db.connect()

dbCon.exec(sql"CREATE TABLE IF NOT EXISTS users (id INTEGER, name VARCHAR);")
dbCon.exec(sql"INSERT INTO users VALUES (1, 'Alice'), (2, 'Bob');")

for row in dbCon.getAllRows("SELECT * FROM users;"):
  echo row

dbConn.disconnect()
db.close()

Data Import

DuckDB can directly connect to many popular data sources and offers several data ingestion methods that allow you to easily and efficiently fill up the database. Supported data sources include CSV, JSON and Parquet files.

import duckdb

var db = open("my_database.duckdb")
var dbCon = db.connect()

for row in dbCon.getAllRows("SELECT * FROM read_json('data.json)"):
  echo row

dbCon.disconnect()
db.close()
DuckDB List/Array

If your JSON contains a list/array, you can cast to VARCHAR to get a JSON string representation of the array.

let res = dbCon.getAllRows(sql"SELECT * EXCLUDE (fruits), fruits::VARCHAR as fruits_json FROM read_json('test.json');")
for row in res.rows:
  echo row.get("fruits_json") # print the JSON string representation of the array

❤ Contributions & Support

🎩 License

MIT license. Made by Humans from OpenPeeps.
Copyright OpenPeeps & Contributors — All rights reserved.