{"id":16613150,"url":"https://github.com/c-cube/sqlite3_utils","last_synced_at":"2025-10-29T18:31:02.698Z","repository":{"id":41812127,"uuid":"224563571","full_name":"c-cube/sqlite3_utils","owner":"c-cube","description":"[beta] High-level wrapper around ocaml-sqlite3","archived":false,"fork":false,"pushed_at":"2022-04-28T14:43:19.000Z","size":86,"stargazers_count":17,"open_issues_count":1,"forks_count":0,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-02-07T21:35:37.390Z","etag":null,"topics":["gadt","ocaml","query","sql","sqlite3"],"latest_commit_sha":null,"homepage":"https://c-cube.github.io/sqlite3_utils/","language":"OCaml","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/c-cube.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-11-28T03:34:49.000Z","updated_at":"2024-11-04T23:39:35.000Z","dependencies_parsed_at":"2022-08-11T18:11:04.325Z","dependency_job_id":null,"html_url":"https://github.com/c-cube/sqlite3_utils","commit_stats":null,"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/c-cube%2Fsqlite3_utils","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/c-cube%2Fsqlite3_utils/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/c-cube%2Fsqlite3_utils/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/c-cube%2Fsqlite3_utils/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/c-cube","download_url":"https://codeload.github.com/c-cube/sqlite3_utils/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":238864405,"owners_count":19543533,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gadt","ocaml","query","sql","sqlite3"],"created_at":"2024-10-12T01:46:07.878Z","updated_at":"2025-10-29T18:30:57.419Z","avatar_url":"https://github.com/c-cube.png","language":"OCaml","readme":"# Sqlite3_utils [![Actions Status](https://github.com/c-cube/sqlite3_utils/workflows/build/badge.svg)](https://github.com/c-cube/sqlite3_utils/actions)\n\n`Sqlite3_utils` is a high-level wrapper around the\n[sqlite3 bindings](https://github.com/mmottl/sqlite3-ocaml)\nwith utils and helpers functions to manage resources and handle typing and statements.\n\n## Docs\n\n[online docs](https://c-cube.github.io/sqlite3_utils/)\n\n## Examples\n\nA few examples to illustrate basic usage of this library.\nLet's assume you have installed the library and run:\n\n```ocaml\n# #require \"sqlite3_utils\";;\n# open Sqlite3_utils;;\n```\n\nMost functions come with `f` and `f_exn` versions, the latter raising `RcError rc`\nwhere Sqlite returns the error code `rc`, the former returning a `('a, Rc.t) result`.\n\n### Executing non parametrized statements\n\nHere we use `with_db` to open a new handle and run some code with this\nhandle, ensuring the handle is closed when our code returns (no resource leak).\nThe function `exec0_exn` is a convenient form for running simple statements\nthat take no parameters and return no values, and `exec_raw_args` deal with `Sqlite3.Data.t`\nvalues for both parameters and values returned by the cursor:\n\n```ocaml\n# with_db \":memory:\" (fun db -\u003e\n   exec0_exn db \"create table person (name text, age int);\";\n   exec0_exn db \"insert into person values ('alice', 20), ('bob', 25) ;\";\n   exec_raw_args db \"select age from person where name=? ;\" [| Data.TEXT \"alice\" |]\n     ~f:Cursor.to_list);;\n- : (Data.t array list, Rc.t) result = Ok [[|Sqlite3_utils.Data.INT 20L|]]\n```\n\n### Typed API\n\nLet us re-consider the previous example but with the typed API:\n\n```ocaml\n# with_db \":memory:\" (fun db -\u003e\n   exec0_exn db \"create table person (name text, age int);\";\n   exec0_exn db \"insert into person values ('alice', 20), ('bob', 25) ;\";\n   exec db \"select age from person where name=? ;\"\n    ~ty:Ty.([text], [int], (fun (x:int) -\u003e x))\n    \"alice\"\n     ~f:Cursor.to_list);;\n- : (int list, Rc.t) result = Ok [20]\n```\n\nWe provide a `~ty` argument that, in the most general case, `exec`,\nfor a parametrized statement that returns values, defines the type\nof parameters and the type of return values.\nHere `ty` is a triple `(type of params, type of result columns, function f)`\nwhere `f` turns the list of returned columns into a single value. `f`\ncan typically be used to build a tuple or record for each result row.\n\nThe module `Sqlite3_utils.Ty` contains combinators for declaring types\n(base types: `text`, `int`, `blob`, etc. and composition operators including\n`::` so that one can one `[int; text]`)\nas well as for making simple tuples.\n\n### Computing the fibonacci function\n\nWe can use sqlite to compute recursive functions with the\n[`WITH RECURSIVE` form](https://www.sqlite.org/lang_with.html):\n\n```ocaml\n# let fib n =\n  let q = \"with recursive fib(a,b,c) as\n    ( values (1,1,1),(2,1,2) UNION select a+1, c, b+c from fib where a\u003c=?)\n    select c from fib where a = ?;\"\n  in\n  with_db \":memory:\" (fun db -\u003e\n      let l =\n        exec db q ~ty:Ty.([int; int], [int], p1 int, id)\n          n n ~f:Cursor.to_list\n      in\n      match l with\n      | Ok [n] -\u003e n\n      | _ -\u003e assert false\n    )\n    ;;\n\n# fib 10;;\n- : int = 89\n# fib 15;;\n- : int = 987\n```\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fc-cube%2Fsqlite3_utils","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fc-cube%2Fsqlite3_utils","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fc-cube%2Fsqlite3_utils/lists"}