无法通过一个列名称选择现有行

时间:2011-09-11 23:47:03

标签: sql sqlite where

我有一个带有名为users的表的sqlite3数据库。该表有一个名为activation_token的列,其类型为VARCHAR(255)

此表中有一行使用

填充此列
900395b3d2faf7d553f719df666d1a755fb7aef0

我希望以下内容能够返回该记录,但我没有输出:

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

实际上这个命令的输出确实让我困惑

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

900395b3d2faf7d553f719df666d1a755fb7aef0

我做错了什么?

输出.schema users以验证我的列名是否正确:

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_users_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");

输出SELECT '->' || activation_token || '<-' FROM users;以验证没有空格:

->900395b3d2faf7d553f719df666d1a755fb7aef0<-

3 个答案:

答案 0 :(得分:2)

要检查activation_token列中的实际值,我建议如下:

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

如果在值的开头或结尾有一些空格,百分号应该很明显。

答案 1 :(得分:1)

您的activation_token值为'900395b3d2faf7d553f719df666d1a755fb7aef0',其上有一些尾随空格。例如:

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      <-

可能会对SQLite邮件列表中的这篇文章感兴趣:

  

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

PostgreSQL 9表现出相同的行为,MySQL 5.1保留了空格但忽略了它们进行比较;可能有配置选项来改变这种行为。我没有其他任何方便的东西,所以我无法检查任何其他人。

答案 2 :(得分:0)

在您的第二个查询中,当您基于SELECT进行限制时,您activation_code会有一个名为activation_token的列。我认为您的两个列都存在并且在查询中混淆了它们。