SQLite3 gem中的类型转换有什么作用?

时间:2017-11-21 18:53:01

标签: sql ruby rubygems

gem的网站上没有记录初始化方法中的

type_translation = true。它做了什么?

2 个答案:

答案 0 :(得分:3)

从此链接:https://github.com/sparklemotion/sqlite3-ruby/blob/master/lib/sqlite3/database.rb

" ... Database类也提供类型转换服务,SQLite3数据类型(全部表示为字符串)可以通过它转换为相应的类型(如表中的模式中所定义) )。此查询仅在从数据库查询数据时发生 - 插入和更新仍然是无类型的..."

这也可能有所帮助:https://sqlite.org/datatype3.html因为它解释了如何在sqlite3中处理数据类型。

答案 1 :(得分:3)

答案可以在examining the source code找到:

# Return the type translator employed by this database instance. Each
# database instance has its own type translator; this allows for different
# type handlers to be installed in each instance without affecting other
# instances. Furthermore, the translators are instantiated lazily, so that
# if a database does not use type translation, it will not be burdened by
# the overhead of a useless type translator. (See the Translator class.)

当您查看Translator class

# The Translator class encapsulates the logic and callbacks necessary for
# converting string data to a value of some specified type. Every Database
# instance may have a Translator instance, in order to assist in type
# translation (Database#type_translation).

因此,当type_translation = true时,sqlite3将使用该数据库的类型转换器来协助在数据类型之间序列化数据。