gpt4 book ai didi

shell - 推断 SQLite3 shell 工具返回的值的类型

转载 作者:行者123 更新时间:2023-12-03 19:36:02 28 4
gpt4 key购买 nike

我没有必要使用 SQLite3 shell 工具来维护一个小型数据库。我正在使用 -header -ascii标志,尽管据我所知,这适用于任何输出选择。我正在寻找一种方法来避免对返回的任何一个值的类型产生歧义。考虑以下:

Create Table `things` (`number` Integer, `string` Text, `binary` Blob);
Insert Into `things` (`number`,`string`,`binary`) Values (4,'4',X'34');
Select * From `things`;

这将返回(使用 caret notation ):
number^_string^_binary^^4^_4^_4^^

很明显,没有办法仅从响应中推断任何“4”字符的类型,因为它们都没有区分分隔符。

有没有办法强制将类型元数据包含到响应中?

我想避免:
  • 更改查询语句以包含类型,因为这会造成混淆,并且在我切换接口(interface)的情况下是多余的;
  • 在插入之前为 TEXT 和 BLOB 值添加前缀,因为这对于所有 TEXT 和 BLOB 交互都必须是统一的(也就是说,如果遇到这种情况,这仍然是我的首选)。

  • 我正在寻找的是某种类型的开关,它指示类型作为 SQLite 响应的一部分,例如:
    number^_string^_binary^^4^_'4'^_X'4'^^
    number^_string^_binary^^4^_text:4'^blob:4^^

    或者它的一些变体。其基础是响应本身包含足够的信息来识别响应中每个元素的类型和值(与 SQLite 库 API 中 sqlite3_column_type() 允许的方式非常相似)。

    更新:自从@mike-sherrill-cat-recall 第一次回答以来,我已经完善了这个问题以澄清期望。

    最佳答案

    在 SQLite 中,回显列的数据类型并不总是有意义。 SQLite 没有传统意义上的按列数据类型。您可以使用 typeof(X)在 SQL 中显示“表达式 X 的数据类型”。

    sqlite> create table test (n integer, d decimal(8, 2));
    sqlite> insert into test (n, d) values (8, 3.14);

    sqlite> insert into test (n, d) values ('wibble', 'wibble');

    将文本插入整数列成功。
    sqlite> select n, typeof(n), d, typeof(d) from test;
        n           typeof(n)   d           typeof(d)     ----------  ----------  ----------  ----------    8           integer     3.14        real          wibble      text        wibble      text      

    You can concatenate anything you like--even producing caret notation--but it's kind of clumsy.

    sqlite> select '(' || typeof(n) || ')^_' || n as caret_n from test;

    caret_n
    -------------------------
    (整数)^_8
    (文字)^_wibble

    SQLite Core Functions

    关于shell - 推断 SQLite3 shell 工具返回的值的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34844861/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com