首页 > 解决方案 > 我应该如何在 MySQL 中存储数据以快速搜索和修改?

问题描述

我需要存储一个数组VAR_CHAR(16)(这些字符串可能只包含、A-Za-z)。第一列是唯一的。第二列是一个数组本身。0-9_uniqueIdAUTO_INCREMENT INT

我到底需要什么:我在伪shell中运行一些命令,例如:names add <for whom> <name>,它应该:

伪代码(像 java,但不是):

String for_whom = ""; // <for_whom> from the command
String name = ""; // <name> from the command

ResultSet rs = findArrayContaining(for_whom);
if (rs.next()) {
    int uniqueID = rs.getInt(1);
    String[] array = columnToStrings(rs.get?????(2));

    // Sorry, I don't know any better ways to add element to array
    List<String> list = Arrays.asList(array);
    list.add(name);
    array = list.toArray(new String[0]);
    rs.close();

    // now we need to somehow
    String str = convertToString(array);
    // and execute
    // "UPDATE `sometable` SET `names`='" + str + "' WHERE `uniqueId`=" + id

} else {
    String[] array = new String[] { for_whom, name };
    // now we need to somehow
    String str = convertToString(array);
    // and execute
    // "INSERT INTO `sometable` (`uniqueId`, `names`) VALUES ('" + str + "', " + id + ")"
}

当我在伪shell中运行以下命令时names get <whose>,它应该:

伪代码(像 java,但不是):

String whose = ""; // <whose> from the command

ResultSet rs = findArrayContaining(whose);
if (rs.next())
    System.out.println("Names of user '" + whose + "': " + String.join(", ", columnToStrings(rs.get?????(2))) + ".");
else
    System.out.println("User '" + whose + "' has only 1 name.");

rs.close();

所以我不知道如何存储这些数据以及如何快速搜索我需要的条目。

我尽可能地描述了我的任务。我希望我能得到答案。提前致谢。如果需要,请向我询问更多详细信息,告诉我您需要哪些详细信息。 对不起,如果我的英语不好,我总是努力提高它

更新:这些值是用户名,没有任何指定值的列表。

标签: javamysqldatabasejdbc

解决方案


推荐阅读