sqlite 无类型列和 .db 文件中的存储
我有一个 SQlite3 表,其中包含无类型列,如下例所示:
CREATE TABLE foo(
Timestamp INT NOT NULL,
SensorID,
Value,
PRIMARY KEY(Timestamp, SensorID)
);
我有特定原因不声明列 SensorID
和 Value
的类型。 当插入带有数字SensorID
和Value
列的行时,我注意到它们被以纯文本写入< code>.db 文件。
当我将 CREATE TABLE 语句更改为...
CREATE TABLE foo(
Timestamp INT NOT NULL,
SensorID INT,
Value REAL,
PRIMARY KEY(Timestamp, SensorID)
);
...那么这些值似乎以某种二进制格式写入 .db 文件。
由于我需要向数据库写入数百万行,因此我担心该数据产生的文件大小,因此希望避免以纯文本形式存储值。
我可以强制 SQLite 在其数据库文件中使用二进制表示形式而不使用显式类型化列吗?
注意:行当前是使用 PHP::PDO 使用准备好的语句编写的。
I have a SQlite3 table that has typeless columns like in this example:
CREATE TABLE foo(
Timestamp INT NOT NULL,
SensorID,
Value,
PRIMARY KEY(Timestamp, SensorID)
);
I have specific reasons not to declare the type of the columns SensorID
and Value
.
When inserting rows with numeric SensorID
and Value
columns I notice that they are being written as plain text into the .db
file.
When I change the CREATE TABLE
statement to...
CREATE TABLE foo(
Timestamp INT NOT NULL,
SensorID INT,
Value REAL,
PRIMARY KEY(Timestamp, SensorID)
);
...then the values seem to be written in some binary format to the .db file.
Since I need to write several millions of rows to the database, I have concerns about the file size this data produces and so would like to avoid value storage in plain text form.
Can I force SQLite to use binary representation in it's database file without using explicitly typed columns?
Note: Rows are currently written with PHP::PDO using prepared statements.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有关类型的 sqlite 文档 中第 3.4 节中的示例演示了将数字作为 int 插入列中,而无需类型的显式声明。我想诀窍是省略数字周围的引号,这会将其转换为字符串(在类型列的情况下,该字符串将被强制恢复为数字)。
上面链接的页面中的第 2 部分还提供了有关正在发生的类型转换的大量信息。
The example in section 3.4 in the sqlite docs about types demonstrates the insertion of a number as int in a column without an explicit declaration of type. I guess the trick is leaving out the quotes around the number, which would convert it to a string (which, in the case of typed columns, would be coerced back into a number).
Section 2 in the page linked above also provides a lot of info about the type conversions taking place.