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

发布于 2024-12-03 19:33:40 字数 1924 浏览 0 评论 0 原文

我有一个 sqlite3 数据库,其中有一个名为 users 的表。该表有一个名为 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 '->' 的输出||激活令牌 || '<-' FROM users; 来验证没有空格:

->900395b3d2faf7d553f719df666d1a755fb7aef0<-

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

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

->900395b3d2faf7d553f719df666d1a755fb7aef0<-

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

想念有你 2024-12-10 19:33:41

要检查 activate_token 列中实际存在的值,我建议执行以下操作:

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

如果值的开头或结尾有一些空格,百分号应该显而易见。

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.

轮廓§ 2024-12-10 19:33:41

您的 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/ [电子邮件受保护]/msg30848.html

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

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/[email protected]/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.

半仙 2024-12-10 19:33:41

在第二个查询中,您SELECT名为activation_code的列,同时根据activation_token进行限制。我认为您同时存在这两列,并且在您的查询中混淆了它们。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文