#sqlite #rusqlite #pretty-print #database

pretty-sqlite

使用 rusqlite 的简单、极简 SQLite 格式化打印

1 个不稳定版本

0.0.1 2024年4月16日

#32#rusqlite

MIT/Apache

13KB
146

pretty-sqlite - 使用 rusqlite 的简单、极简 SQLite 格式化打印

该库通过方便地将表或 select 语句的内容打印成格式良好的表格,辅助进行 SQLite 的测试和探索性开发,使用 tabled crate。

该库基于 rusqlite crate。

注意

  • 版本 0.0.x 将进行 API 变更。请随意选择您需要的代码。
  • 即将推出的版本 0.1.x 将更加稳定,但仍可能包含一些 API 破坏性变更。因此,建议锁定到特定版本(例如,=0.1.2)。
  • 版本 0.2.x 及以上将更严格遵循语义版本控制。

重要提示:默认情况下,所有查询/打印都限制在 300 条记录。使用 pretty_selection_with_optionsPrettyOptions 可以更改此默认行为。

API 习惯用法如下

  • pretty_... 前缀的函数,如 pretty_tablepretty_select,返回表/查询内容的格式化字符串。
  • print_... 前缀的函数,如 print_tableprint_select,在上述函数上调用 println!()

还有一个更高级的函数

  • pretty_select_with_options(conn, sql, params, pretty_options) 允许使用 PrettyOptions 定制表结果。

示例

let conn = Connection::open_in_memory()?; // for file: Connection::open(path)?
// ... seed db

// -- Print table
pretty_sqlite::print_table(&conn, "person")?;

// Same as:
// let content = pretty_sqlite::pretty_table(&conn, "person")?;
// println!("{content}");

将打印类似的内容

 TABLE: person
┌────┬────────────┬──────┬──────────┬───────────────────┐
│ id │ name       │ yob  │ data_t   │ data_b            │
├────┼────────────┼──────┼──────────┼───────────────────┤
│ 1"Person 1"1951"Data 1"BLOB (length: 10) │
├────┼────────────┼──────┼──────────┼───────────────────┤
│ 2"Person 2"1952"Data 2"BLOB (length: 10) │
├────┼────────────┼──────┼──────────┼───────────────────┤
│ 3"Person 3"1953"Data 3"BLOB (length: 10) │
├────┼────────────┼──────┼──────────┼───────────────────┤
│ 4"Person 4"1954"Data 4"BLOB (length: 10) │
├────┼────────────┼──────┼──────────┼───────────────────┤
│ 5"Person 5"1955"Data 5"BLOB (length: 10) │
└────┴────────────┴──────┴──────────┴───────────────────┘
let conn = Connection::open_in_memory()?; // for file: Connection::open(path)?
// ... seed db

pretty_sqlite::print_select(&conn, "select id, name, yob from person where id > ?", (2,))?;

// Same as:
// let content = pretty_sqlite::pretty_select(&conn, "select id, name, yob from person where id > ?", (2,))?;
// println!("{content}");
┌────┬────────────┬──────┐
│ id │ name       │ yob  │
├────┼────────────┼──────┤
│ 3"Person 3"1953 │
├────┼────────────┼──────┤
│ 4"Person 4"1954 │
├────┼────────────┼──────┤
│ 5"Person 5"1955 │
└────┴────────────┴──────┘

请参阅 examples/readme.rs

依赖项

~24MB
~459K SLoC