views:

33

answers:

2

The table: CREATE TABLE configuration(Key STRING, Value STRING, PRIMARY KEY (Key) );

Here is what I tried: insert into configuration(Key,Value) values(42,cast('0042' as text));

Here is the dump: INSERT INTO "configuration" VALUES(42,42);

What I wanted: INSERT INTO "configuration" VALUES(42,'0042');

A: 

I'm not sure what you are trying, but essentially, it should be:

INSERT INTO configuration VALUES(42,'0042');
MPelletier
That has no effect.
Donal Fellows
Sorry, configuration should not be between quotes.
MPelletier
That makes no difference. (The quotes were fine; that's SQL syntax for a named entity such as column or field where the name can't be used “bare”.) Really. I just type this into an interactive session and get the wrong behavior…
Donal Fellows
+1  A: 

If you create the table with:

CREATE TABLE configuration(Key STRING, Value TEXT, PRIMARY KEY (Key) );

(there is no storage penalty for doing this with SQLite) then you'll get the leading zeroes preserved, even if you use the very simplest form of INSERT. This is because STRING is not a real SQLite type, and so has NUMERIC affinity.

Donal Fellows
Not for SQLite. `STRING` will be interpreted to `TEXT`. So would `VARCHAR`, `VARCHAR2`, and `POTATO`. (I kid you not).
MPelletier
@MPelletier: I know that, but I also know that when I use `TEXT` it works and when I use `STRING` it doesn't. I simply tested it.
Donal Fellows
@MPelletier: Hah. It's *documented* that STRING is NUMERIC (see edited-in link). That's a real nasty gotcha!
Donal Fellows
@Donal Fellows, Nice! I hadn't picked up on that.
MPelletier
Thanks, that is exactly what I wanted !
ybart