开发者

Can't select existing row via one column name

I have a sqlite3 database with a table called users. The table has a column called activation_token which is of type VARCHAR(255).

There is a single row in this table populates this column with

900395b3d2faf7d553f719df666d1a755fb7aef0

I would expect the following to return that record but I get no output:

SELECT * FROM users 
WHERE activation_token = '900395b3d2faf7d553f719df666d1a755fb7aef0';

In fact this command's output really confuses me

SELECT activation_token FROM users 
where activation_token != '900395b3d2faf7d553f719df666d1a755fb7aef0';

900395b3d2faf7d553f719df666d1a755fb7aef0

What am I doing wrong?

Output of .schema users to verify that I'm getting the column name correct:

CREATE TABLE "users" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "email" varchar(255), "crypted_password" varchar(255), "salt" varchar(255), "created_at" datetime, "updated_at" datetime, "remember_me_token" varchar(255) DEFAULT NULL, "remember_me_token_expires_at" datetime DEFAULT NULL, "activation_state" varchar(255) DEFAULT NULL, "activation_token" varchar(255) DEFAULT NULL, "activation_token_expires_at" datetime DEFAULT NULL, "reset_password_token" varchar(255) DEFAULT NULL, "reset_password_token_expires_at" datetime DEFAULT NULL, "reset_password_email_sent_at" datetime DEFAULT NULL, "last_login_at" datetime DEFAULT NULL, "last_logout_at" datetime DEFAULT NULL, "last_activity_at" datetime DEFAULT NULL, "failed_logins_count" integer DEFAULT 0, "lock_expires_at" datetime DEFAULT NULL);
CREATE INDEX "index_users_on_activation_token" ON "users" ("activation_token");
CREATE INDEX "index_us开发者_运维技巧ers_on_last_logout_at_and_last_activity_at" ON "users" ("last_logout_at", "last_activity_at");
CREATE INDEX "index_users_on_remember_me_token" ON "users" ("remember_me_token");

Output of SELECT '->' || activation_token || '<-' FROM users; to verify that there is no whitespace:

->900395b3d2faf7d553f719df666d1a755fb7aef0<-


To check what value is actually in the activation_token column I would recommend the following:

SELECT '%' || activation_token || '%' FROM users WHERE 1=1;

The percent signs should make it obvious if there is some whitespace at the beginning or end of the value.


You have an activation_token value that is '900395b3d2faf7d553f719df666d1a755fb7aef0' with some trailing spaces on it. For example:

sqlite> create table pancakes (activation_token varchar(255));
sqlite> insert into pancakes values('900395b3d2faf7d553f719df666d1a755fb7aef0');
sqlite> insert into pancakes values('900395b3d2faf7d553f719df666d1a755fb7aef0      ');

sqlite> select activation_token from pancakes;
activation_token
900395b3d2faf7d553f719df666d1a755fb7aef0
900395b3d2faf7d553f719df666d1a755fb7aef0      

sqlite> select '->' || activation_token || '<-' from pancakes;
'->' || activation_token || '<-'
->900395b3d2faf7d553f719df666d1a755fb7aef0<-
->900395b3d2faf7d553f719df666d1a755fb7aef0      <-

sqlite> select '->' || activation_token || '<-' from pancakes where activation_token = '900395b3d2faf7d553f719df666d1a755fb7aef0';
'->' || activation_token || '<-'
->900395b3d2faf7d553f719df666d1a755fb7aef0<-

sqlite> select '->' || activation_token || '<-' from pancakes where activation_token != '900395b3d2faf7d553f719df666d1a755fb7aef0';
'->' || activation_token || '<-'
->900395b3d2faf7d553f719df666d1a755fb7aef0      <-

This post from the SQLite mailing list might be of interest:

http://www.mail-archive.com/sqlite-users@sqlite.org/msg30848.html

PostgreSQL 9 exhibits the same behavior, MySQL 5.1 preserves the spaces but ignores them for comparisons; there may be configuration options to alter this behavior. I don't have anything else handy so I can't check any others.


In your second query, you SELECT a column called activation_code while you restrict based on activation_token. I think you have both columns present and confuse them in your queries.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜