TCL SQLITE查询返回带有空格的文本字段(列表 vs. 非列表)

huangapple go评论59阅读模式
英文:

TCL SQLITE query return for text fields with spaces (list vs. non-list)

问题

When performing a tcl sqlite extension query where there is a single result that is a string with spaces, the return value can either be a string or a list depending on how the query is evaluated.

SQLite's eval function returns the result as a list:

{space separated string}

However, when using onecolumn, it returns a string value:

space separated string

The reason for this distinction is that eval returns results in a way that preserves multiple columns and rows, even for a single result. On the other hand, onecolumn is designed to simplify and return just a single value, so it returns that value as a string without wrapping it in a list. This behavior is to provide flexibility in handling different query scenarios.

英文:

When performing a tcl sqlite extension query where there is a single result that is a string with spaces, the return value can either be a string or a list depending on how the query is evaluated. Why is there a distinction and what should be the expected behavior?

Example of TCL session with sqlite extension:

package require sqlite3

sqlite3 db :memory:

db eval {select 'space separated string'}

results in a list:

{space separated string}

however, using onecolumn, it returns a string value

db onecolumn {select 'space separated string'}

result

space separated string

Why does the sqlite eval return a list and onecolumn return a string?

答案1

得分: 1

它被定义为这样工作。文档中说:

># "onecolumn" 方法
>"onecolumn" 方法的工作方式类似于 "eval",它会评估作为参数给出的 SQL 查询语句。不同之处在于,"onecolumn" 返回一个单一元素,该元素是查询结果的第一行的第一列。
>
>这是一个便利的方法。它避免了用户必须在 "eval" 的结果上执行 "[lindex ... 0]" 以提取单列结果的操作。

也就是说,"onecolumn" 方法只返回结果集的第一行的第一列(或者在没有结果产生时返回布尔值)。当您编写一个只产生一行并且只有一列的查询时,它会检索该值,因此非常方便用于“评估表达式”的场景。它检索的值在 SQL 方面始终是一个简单的值;您的简单值恰好包含空格,所以它们自然出现在 "onecolumn" 的结果中。

英文:

It's defined to work that way. The documentation says:

># The "onecolumn" method
>The "onecolumn" method works like "eval" in that it evaluates the SQL query statement given as its argument. The difference is that "onecolumn" returns a single element which is the first column of the first row of the query result.
>
>This is a convenience method. It saves the user from having to do a "[lindex ... 0]" on the results of an "eval" in order to extract a single column result.

That is, a onecolumn method returns just the first column of the first row of the result set (or a boolean when no result is produced). When you're writing a query that produces only one row and only one column in that row, it retrieves that value and so is very convenient for the "evaluate an expression" scenario. The value it retrieves is always a simple value in an SQL sense; your simple value happens to include spaces so naturally they're in the result of onecolumn.

huangapple
  • 本文由 发表于 2023年5月26日 09:40:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76337158.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定