从SQLite表中选择一条记录 - 并检查其时间戳是否是最新的

时间:2015-10-21 11:01:26

标签: sqlite android-sqlite

在Android游戏中,我将从不同社交网络中检索到的用户信息存储在SQLite表中 -

create table table_social (
        id text primary key,
        social integer not null, /* Facebook = 1, Google+ = 2, Twitter = 3, ... */
        first_name text not null,
        photo text null,
        city text null,
        stamp datetime default current_timestamp /* indicates if it's "main" profile */
);

并按最新时间戳显示“主要”玩家个人资料(应用导航抽屉和远程游戏合作伙伴中显示的个人资料):

app screenshot

我的问题:

是否可以从这样的表中选择某条记录,并在相同的SQL命令中指明它是否具有最新的时间戳?

(我想使用后一种信息启用/禁用我的应用中的“设置为主要配置文件”按钮。)

以下是我在Android SDK的 sqlite3.exe 提示下尝试过的内容 -

首先创建表并用3条记录填充它:

C:\Users\user1\AppData\Local\Android\Sdk\platform-tools>sqlite3.exe
SQLite version 3.8.10.2 2015-05-20 18:17:19
Enter ".help" for usage hints.
Connected to a transient in-memory database.
Use ".open FILENAME" to reopen on a persistent database.
sqlite> create table table_social (
   ...>         id text primary key,
   ...>         social integer not null, /* Facebook = 1, Google+ = 2, Twitter = 3, ... */
   ...>         first_name text not null,
   ...>         photo text null,
   ...>         city text null,
   ...>         stamp datetime default current_timestamp
   ...> );
sqlite> insert into table_social (id, social, first_name) values (1, 1, "Alex on Facebook");
sqlite> insert into table_social (id, social, first_name) values (2, 2, "Alex on Google+");
sqlite> insert into table_social (id, social, first_name) values (3, 3, "Alex on Twitter");

然后我可以检索“主要”玩家资料:

sqlite> select * from table_social order by stamp desc limit 1;
3|3|Alex on Twitter|||2015-10-21 10:47:40

我能够检索Facebook用户数据:

sqlite> select *, stamp=max(stamp) from table_social where social=1;
1|1|Alex on Facebook|||2015-10-21 10:47:18|1

但是如何组合这两个命令呢?

我尝试了以下操作,但它返回1(虽然它应该返回 false ):

sqlite> select *, max(stamp)=stamp from table_social where social=1;
1|1|Alex on Facebook|||2015-10-21 10:47:18|1

我也尝试了一个子选择查询:

sqlite> select *, stamp=(select * from table_social order by stamp desc limit 1) from table_social where social=1;
Error: only a single result allowed for a SELECT that is part of an expression

1 个答案:

答案 0 :(得分:1)

标量子查询必须返回单个值。 (错误消息已经告诉过你。)所以用Step 1: Open terminal :) Step 2: Open .bash_profile in vim to edit: > vi ~/.bash_profile Step 3: Add line: > export GRADLE_HOME=/usr/local/opt/gradle (for me it was this location, it can be different for u) Step 4: Add line: > export PATH="$PATH:$GRADLE_HOME/bin" (to export bin directory of gradle) Step 5: Verify by reopening the terminal or new tab, and check by echoing: > echo $GRADLE_HOME 替换(select * ...)