如何在顶部表格上插入项目

时间:2017-03-27 13:46:09

标签: postgresql jdbc

如何在PostgreSQL中的顶级表上插入项目?那有可能吗?在表格中,我只有两个字段作为文本。首先是主键。

CREATE TABLE news_table (
    title text not null primary key,
    url text not null    
);

我需要在java中对程序进行简单查询。

好的,这是我的代码:

get("/getnews", (request, response) -> {
    List<News> getNews = newsService.getNews();
    List<News> getAllNews = newsService.getAllNews();
    try (Connection connection = DB.sql2o.open()) {
        String sql = "INSERT INTO news_table(title, url) VALUES (:title, :url)";
        for (News news : getNews) {
            if (!getAllNews.contains(news)) {
                connection.createQuery(sql, true)
                        .addParameter("title", news.getTitle())
                        .addParameter("url", news.getUrl())
                        .executeUpdate()
                        .getKey();
            }

        }
    }
    return newsService.getNews();
}, json());

问题是,因为它第二次调用getnews方法,这个新消息在表的末尾添加,并且没有现存的hronologi新闻。这怎么解决?我使用Sql2o + sparkjava。

可能我已经知道了。在我必须包含对象getnews和getallnews之前,我需要反转List getnews吗?

2 个答案:

答案 0 :(得分:3)

表格中没有开头或结尾。如果要对数据进行排序,只需在SELECT语句中使用ORDER BY即可。没有ORDER BY,就没有订单。

答案 1 :(得分:1)

关系理论是关系数据库的数学基础,它规定了关系(在真实数据库中表示为表格)必须遵守的某些条件。其中之一是它们没有排序(,行既不会以任何特定顺序存储也不会被检索,因为它们被视为数学集)。因此,它完全处于RDBMS的控制之下,在该表中将新行输入。

因此,在检索数据时,无法使用ORDER BY子句确保数据的特定排序。

相关问题