0

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');

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Not for SQLite. STRING will be interpreted to TEXT. So would VARCHAR, VARCHAR2, and POTATO. (I kid you not).
@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.
@MPelletier: Hah. It's documented that STRING is NUMERIC (see edited-in link). That's a real nasty gotcha!
Nice! I hadn't picked up on that.
1

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

INSERT INTO configuration VALUES(42,'0042');

2 Comments

Sorry, configuration should not be between quotes.
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…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.