5

我对 sqlite3 中的数据类型有疑问。

由于值SQLITE_INTEGER可以存储在 1、2、3、4、6 或 8 个字节中,具体取决于值的大小,如果我只知道 SQlite 数据库中存储的列,SQLITE_INTEGER我怎么知道该列中的值是 4 字节还是 6-8 字节整数,或者应该使用哪一个来获取值,sqlite3_column_int()或者sqlite3_column_int64()

我可以sqlite3_column_bytes()在这种情况下使用吗?但根据文档,sqlite3_column_bytes()主要用于 TEXT 或 BLOB。

谢谢!

4

1 回答 1

2

When SQLite steps into a record, all integer values are expanded to 64 bits.
sqlite3_column_int() returns the lower 32 bits of that value without checking for overflows.

When you call sqlite3_column_bytes(), SQLite will convert the value to text, and return the number of characters.

You cannot know how large an integer value is before reading it. Afterwards, you can check the list in the record format documentation for the smallest possible format for that value, but if you want to be sure that integer values are never truncated to 32 bits, you have to always use sqlite3_column_int64(), or ensure that large values get never written to the DB in the first place (if that is possible).

于 2012-12-12T07:55:50.330 回答