使用查询语言

时间:2015-07-30 22:29:09

标签: spring crud couchbase

我正在使用couchbase 4.0测试版。我成功地使用休息服务将对象保存到桶中。现在我需要访问存储桶并使用查询执行GET,UPDATE和DELETE操作。

这是我的样本对象。

{
       "id": "friend$43403778",
       "domain": "FRIEND",
       "profileId": 43403778,
       "screenName": "49ers",
       "name": "San Francisco 49ers",
       "profileUrl": "http://twitter.com/49ers",
       "description": "Official Twitter account of the five-time Super Bowl Champion San Francisco #49ers.",
       "friendsCount": 121,
       "followersCount": 964650,
       "timeZone": "Pacific Time (US & Canada)",
       "utcOffset": -25200,
       "location": "Santa Clara, CA",
       "profileCreated": 1243629277000
   }

现在我想使用 id 来获取此对象。但 id值包含特殊字符,因此我的查询不适用于它。我怎样才能访问它?

这是我的代码片段,我试图得到它。它没有给我300错误代码语法错误。

        String strQuery = "SELECT * FROM twitter WHERE id=" + id;
        Bucket bucket = singletonBucket.getBucket();
        QueryResult queryResult = bucket.query(Query.simple(strQuery));
        List<QueryRow> result = Collections.emptyList();

        if (!queryResult.finalSuccess()) {
            log.error("Error occur while retrieving TwitterProfile document for id: {}",id);
            String errorStr = "";
            for (JsonObject error : queryResult.errors()) {
                errorStr = error.toString() + ". ";
                log.error("{}", error);
            }
            throw new DataAccessException(errorStr);
        } else {
            result = queryResult.allRows();
            for(QueryRow row: result){
                Object doc = row.value().get(AppConstants.BUCKET_NAME);
                TwitterProfile twitterProfile = objectMapper.readValue(doc.toString(), TwitterProfile.class);
                twitterProfileList.add(twitterProfile);
            }
            log.info("Successfully found {} TwitterProfile record for id {}",twitterProfileList.size(), id);
            return twitterProfileList.size()>0 ?twitterProfileList.get(0):"";
        }

如果我尝试使用 profileId 来获取记录它也不起作用。 如何为此存储桶编写简单查询。 这些是我试过的查询。我的桶名称也是 twitter

String strQuery = "SELECT * FROM twitter WHERE domain=" + AppConstants.DOMAIN_FRIEND;
String strQuery = "DELETE FROM twitter WHERE domain=" + AppConstants.DOMAIN_FRIEND;

提前致谢。

1 个答案:

答案 0 :(得分:0)

构建查询时,必须使用引号来表明您的id是一个字符串。在构建查询时尝试在id周围添加引号:

String strQuery = "SELECT * FROM twitter WHERE id='" + id + "'";
相关问题