嗨:我正在Linux中使用SQLite(V 3.14.2)。显然,SQLITE允许用户将字符串存储在整数列中(我觉得这令人震惊,但这显然允许)。其中一些值似乎是整数的十六进制。我需要在“ .dump”(带有.mode插入)中清理它。这是一个奇数整数/varchar行为的示例...
sqlite> .mode insert
sqlite> create table foo (col1 integer);
sqlite> insert into foo (col1) values (1),('2'),(0x3),('0x4'),(0xf),('My good dog Moby');
sqlite> select * from foo;
INSERT INTO table(col1) VALUES(1);
INSERT INTO table(col1) VALUES(2);
INSERT INTO table(col1) VALUES(3);
INSERT INTO table(col1) VALUES('0x4');
INSERT INTO table(col1) VALUES(15);
INSERT INTO table(col1) VALUES('My good dog Moby');
如果插入值是int,即使它是insInside单引号,它也会被解释为int。很好,其他DB也这样做。如果值是十六进制的值并且缺乏单引号,那也可以正确解释。到目前为止,一切都很好。但是,如果十六进制值在单个QOTES内部,则没有好处,显然将其存储为某种字符串。
不一定知道哪些表是需要特殊处理的整数的列,是否有一种方法可以使SELECT命令解释用单引号插入的HEX值(只是它将“ 2”解释为上述2)?
如果它完全有帮助,我实际上会在查看数据时使用.dump,而不是选择。
感谢您的帮助!
Hi: I'm working with a SQlITE (v 3.14.2) in linux. Apparently, sqlite allows users to store char strings in integer columns (I find this shocking, but that's what it apparently allows). Some of those values appear to be hex expressions of an integer. I need to clean this up in a ".dump" (with .mode insert). Here's an example of the odd integer/varchar behavior...
sqlite> .mode insert
sqlite> create table foo (col1 integer);
sqlite> insert into foo (col1) values (1),('2'),(0x3),('0x4'),(0xf),('My good dog Moby');
sqlite> select * from foo;
INSERT INTO table(col1) VALUES(1);
INSERT INTO table(col1) VALUES(2);
INSERT INTO table(col1) VALUES(3);
INSERT INTO table(col1) VALUES('0x4');
INSERT INTO table(col1) VALUES(15);
INSERT INTO table(col1) VALUES('My good dog Moby');
If the inserted value is an int, it gets interpreted as an int, even if it'sinside single quotes. That's fine, other DBs do this too. If the value is a hex value and lacks the single quotes, that gets interpreted correctly too. So far, so good. But if the hex value is inside the single qotes, no good, it apparently gets stored as a string of some sort.
Without necessarily knowing which columns of which tables are integers that need special treatment, is there a way to get the select command to interpret hex values that were inserted with single quotes as ints (just the way it interpreted '2' as 2 above) ?
If it helps at all, I'm actually going to be using .dump, not select, when looking at the data.
Thanks for any help !
发布评论
评论(2)
您 can 实际上是从固定基准数字的字符串转换为普通sqlite中的数字值,但是使用a
@choroba答案中的一个会产生一个斑点,而不是整数,而不是想要您想要的。
You can actually convert from a string holding a base-16 number to a numeric value in plain Sqlite, but it's kind of ugly, using a recursive CTE:
gives
If you want to go the C function route, your implementation function should look something like:
except maybe with more error checking. The one in @choroba's answer creates a blob, not an integer, which isn't want you want.
SQLite不会从十六进制转换为DEC,您需要自己编写这样的功能。 https://sqlite.org/forum/info/79dc039e21c6a1ea”中找到一个示例
可以在 0x :
更好的方法是将其修复在存储值的应用程序中。
SQLite doesn't convert from hex to dec, you need to write such a function yourself. An example can be found in the SQLite Forum:
You can then call it on the value if it starts with
0x
:Much better way is to fix this in the application that stores the values.