以下是我的名为feeds
column_name | data_type
-------------+-----------------------------
id | integer
category | text
question | text
answer | text
thumb | text
time | timestamp without time zone
类别将以逗号分隔值表示。我想将所有逗号分隔的值放入一个具有唯一元素的数组中(数组中不会出现相同的类别)。
为实现这一目标,我创建了一个名为category的单独表。当用户发布订阅源时,我将同时填充feeds
表和category
表。
@POST
@Path("/post")
@Consumes(MediaType.APPLICATION_JSON)
public Response postStrMsg(String msg) {
String songString = "requesting";
JSONObject jsonObj = new JSONObject(msg);
try {
Connection connection = DbConnection.getConnection();
PreparedStatement stmt = connection.prepareStatement(
"INSERT INTO feeds ( CATEGORY , TOPIC , DESCRIPTION , THUMB , TIMEFRAME , AUTHOR ) VALUES( ?, ? , ? , ? , now() , ? )");
stmt.setString(1, jsonObj.getString("category"));
stmt.setString(2, jsonObj.getString("question"));
stmt.setString(3, jsonObj.getString("answer"));
stmt.setString(4, jsonObj.getString("thumb"));
stmt.setString(5, jsonObj.getString("author"));
stmt.executeUpdate();
String query = "INSERT INTO CATEGORY (SECTION) VALUES ('" + jsonObj.getString("category") + "');";
PreparedStatement stmts = connection.prepareStatement(query);
stmts.executeQuery();
songString = Utilities.constructJSON( "Posted Successfully",true);
} catch (Exception ex) {
songString = Utilities.constructJSON("Failure", false, ex.getMessage());
}
return Response.status(200).entity(songString).build();
}
当用户想要了解所有类别时,我使用以下代码检索类别。这将解析所有逗号分隔值并过滤所有重复元素,它将提供相应的结果。
查询:
"select min(c.id) as id, t.name from category c " + " cross join "
+ "unnest(string_to_array(c.category, ',')) AS t(name) " + "group by t.name order by 1";
代码
@GET
@Path("getCategories")
@Produces(MediaType.APPLICATION_JSON)
public String getCategories() {
ArrayList<Category> output = null;
try {
Connection connection = getConnection();
Statement stmt = connection.createStatement();
String query = "select min(c.id) as id, t.name from category c " + " cross join "
+ "unnest(string_to_array(c.category, ',')) AS t(name) " + "group by t.name order by 1";
ResultSet rs = stmt.executeQuery(query);
output = new ArrayList<Category>();
while (rs.next()) {
output.add(new Category(rs.getString("name")));
}
} catch (Exception ex) {
ex.printStackTrace();
}
JSONArray jsArray = new JSONArray(output);
return jsArray.toString();
}
问题是,
答案 0 :(得分:2)
我认为你还需要两张桌子。一个用于类别,另一个用于将类别与Feed组合。您应该按如下方式创建类别表:
column_name | data_type
-------------+-----------------------------
id | integer
category | text
和feedCategory表如下:
column_name | data_type
-------------+-----------------------------
id | integer
category_id | integer
feed_id | integer
一个Feed可以有多个类别。因此,您可以根据该表填充最后一个表。
每次从数据库检索时,这不是解析类别文本的最佳方法。如何按类别查询Feed?你要为每个Feed解析类别列,看看你是否有它?