Contents Menu Expand Light mode Dark mode Auto light/dark, in light mode Auto light/dark, in dark mode Skip to content
unihan-db 0.22.0 documentation
Light Logo Dark Logo
unihan-db 0.22.0 documentation
  • Quickstart
  • How-to Guides
    • Use a Custom Database
    • Bootstrap from a Local Archive
    • Query Local Rows
  • API Reference
    • Bootstrap Helpers
    • Importer Internals
    • ORM Tables
  • Explanation
    • Pipeline and Schema
  • Project
    • Contributing
    • Code style
    • Releasing
  • Changelog
  • GitHub

team git-pull / Tony Narlock:

vcs-python vcspull (libvcs), g

tmux-python tmuxp libtmux (mcp, pytest)

cihai unihan-etl (db) cihai (cli)

django django-slugify-processor django-docutils django-search-query (admin)

AI libtmux-mcp agentgrep (mcp)

docs + tests gp-libs gp-sphinx

web social-embed

Back to top
View this page
Edit this page

Query Local Rows¶

Once the schema exists, querying unihan-db is ordinary SQLAlchemy ORM work. The central table is unihan_db.tables.Unhn; related reading, dictionary-location, radical/stroke, source, and index tables hang off each character row.

The small example below stays offline by creating one row directly instead of running the full UNIHAN bootstrap.

>>> from sqlalchemy import create_engine
>>> from sqlalchemy.orm import Session
>>> from unihan_db.bootstrap import to_dict
>>> from unihan_db.tables import Base, Unhn
>>> engine = create_engine("sqlite:///:memory:")
>>> Base.metadata.create_all(engine)
>>> session = Session(engine)
>>> session.add(Unhn(char="好", ucn="U+597D"))
>>> session.commit()
>>> row = session.query(Unhn).filter_by(char="好").one()
>>> row.char, row.ucn
('好', 'U+597D')
>>> to_dict(row)["char"]
'好'
>>> session.close()

Use unihan_db.bootstrap.to_dict() when you need a plain dictionary for display or serialization. For real data, call unihan_db.bootstrap.bootstrap_unihan() before running the query.

Next
API Reference
Previous
Bootstrap from a Local Archive
Copyright © Copyright 2017- cihai software foundation (Tony Narlock)
Made with Sphinx and gp-sphinx (fork of Furo by @pradyunsg)
Source: docs/how-to/query-related-tables.md · Machine-readable: Markdown, raw source, docs.json, llms.txt, llms-full.txt