将数字作为字符串插入到文本列中,SQLite 仍然会删除前导零
我得到以下数字作为字符串: String numberString = "079674839";
当我将此数字插入 SQLite 数据库时,SQLite 自动删除前导零并将字符串存储为 79674839。考虑到亲和性以及该列存储 TEXT,SQLite 不应该存储整个字符串并保留前导零吗?
谢谢
I got the following number as a string: String numberString = "079674839";
When I insert this number into a SQLite DB, SQLite automatically removes the leading zero and stores the string as 79674839. Considering affinity and that the column stores TEXT, shouldn't SQLite store the whole string and keep the leading zero?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
仔细检查您的数据库架构。正如 SQLite Version 3 中的数据类型 中所述,列类型名称会影响值在存储之前的处理方式。
下面是一个使用内存数据库进行演示的 Python 程序:
输出为:
因此,看起来您的列实际上是整数类型。查看架构定义(
sqlite3 database.db .schema
从命令行工作),查看 文档,并确保您使用的是映射到 TEXT 亲和力的类型名称之一。未知类型名称具有 INTEGER 关联性。就我自己而言,我使用的是“STR”,它最终得到默认的 INTEGER 亲和力。我将其更改为“TEXT”,SQLite 开始尊重我的前导零。
Double-check your database schema. As documented on Datatypes in SQLite Version 3, the column type name affects how values are processed before being stored.
Here's a Python program to demonstrate, using an in-memory database:
The output is:
So, it looks like your column is actually an integer type. Take a look at the schema definition (
sqlite3 database.db .schema
works from the command line), look at the documentation again, and make sure you are using one of type names that map to TEXT affinity. Unknown type names get INTEGER affinity.In my own case, I was using 'STR', which ends up with the default INTEGER affinity. I changed it to 'TEXT', and SQLite started respecting my leading zeros.
如果数字位于内联 SQL 代码中的任何位置,请使用单引号将数字括起来(即“079674839”)。另外,如果您以编程方式执行此操作,请确保您没有进行数字转换。
Use single quotes around the number, (i.e., '079674839') if it is anywhere in inline sql code. Also, if you're doing this programatically, make sure that you are not going through a numeric conversion.