Android:SQLite 保存字符串数组?

发布于 2024-12-29 18:32:43 字数 579 浏览 1 评论 0原文

我需要将字符串数组保存到数据库,但它不允许我这样做。这就是我所拥有的:

 public long createEntry(String startTime, String endTime, String[] states) {
         ContentValues initialValues = new ContentValues();
         initialValues.put(START_KEY_TIME , startTime);
         initialValues.put(END_KEY_TIME , endTime);
         initialValues.put(KEY_STATE, states );

         return databaseConnect.insert(DATABASE_TABLE, null, initialValues);
}

但是如果我放入 string[] 状态,它表示内容值无法接受参数。我该如何解决这个问题?我在想我的状态有 7 个东西,我可以有 7 个单独的字符串并在每个字符串中存储内容,然后将所有字符串放回到字符串数组中吗?或者这是不好的做法?

I need to save a string array to the database but it won't let me. This is what I have:

 public long createEntry(String startTime, String endTime, String[] states) {
         ContentValues initialValues = new ContentValues();
         initialValues.put(START_KEY_TIME , startTime);
         initialValues.put(END_KEY_TIME , endTime);
         initialValues.put(KEY_STATE, states );

         return databaseConnect.insert(DATABASE_TABLE, null, initialValues);
}

But if I put string[] states in, it says that content values is not able to take an argument. How do I get around that? I was thinking I have 7 things in states, could I like have 7 separate strings and store stuff in each and then afterwards put all the strings back into an string array? Or would that be bad practice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

高跟鞋的旋律 2025-01-05 18:32:44

您无法将字符串数组保存到数据库中。但你可以使用这个技巧。

1) 所以你必须使用 convertArrayToString(String[] array) 方法将其转换为简单的字符串。这将使用“逗号”连接字符串的所有元素。

2) 当您从数据库检索此字符串时,您可以使用 convertStringToArray(String str) 方法将其转换回字符串数组。此方法将从“逗号”中分割字符串,您将得到原始数组。

public static String strSeparator = "__,__";
public static String convertArrayToString(String[] array){
    String str = "";
    for (int i = 0;i<array.length; i++) {
        str = str+array[i];
        // Do not append comma at the end of last element
        if(i<array.length-1){
            str = str+strSeparator;
        }
    }
    return str;
}
public static String[] convertStringToArray(String str){
    String[] arr = str.split(strSeparator);
    return arr;
}

You cannot save String array into Database. But you can use this trick.

1) So you have to convert it into simple String using convertArrayToString(String[] array) method. This will concatenate all elements of string using 'comma'.

2) When you would retrieve this String back from Database you could convert it back to String array using convertStringToArray(String str) method. This method will split the string from 'comma' and you will get your original array back.

public static String strSeparator = "__,__";
public static String convertArrayToString(String[] array){
    String str = "";
    for (int i = 0;i<array.length; i++) {
        str = str+array[i];
        // Do not append comma at the end of last element
        if(i<array.length-1){
            str = str+strSeparator;
        }
    }
    return str;
}
public static String[] convertStringToArray(String str){
    String[] arr = str.split(strSeparator);
    return arr;
}
晨光如昨 2025-01-05 18:32:44

基于穆罕默德的答案,但处理 StringList 而不是数组,并使用 StringBuilder 而不是分配许多 String在值串联期间:

private static final String LIST_SEPARATOR = "__,__";

public static String convertListToString(List<String> stringList) {
    StringBuilder stringBuilder = new StringBuffer();
    for (String str : stringList) {
        stringBuilder.append(str).append(LIST_SEPARATOR);
    }

    // Remove last separator
    stringBuilder.setLength(stringBuilder.length() - LIST_SEPARATOR.length());

    return stringBuilder.toString();
}

public static List<String> convertStringToList(String str) {
    return Arrays.asList(str.split(LIST_SEPARATOR));
}

Based on Muhammed's answer but handles List of Strings instead of array and uses StringBuilder instead of allocating many Strings during values concatenation:

private static final String LIST_SEPARATOR = "__,__";

public static String convertListToString(List<String> stringList) {
    StringBuilder stringBuilder = new StringBuffer();
    for (String str : stringList) {
        stringBuilder.append(str).append(LIST_SEPARATOR);
    }

    // Remove last separator
    stringBuilder.setLength(stringBuilder.length() - LIST_SEPARATOR.length());

    return stringBuilder.toString();
}

public static List<String> convertStringToList(String str) {
    return Arrays.asList(str.split(LIST_SEPARATOR));
}
断舍离 2025-01-05 18:32:44

将其转换为单个字符串以将其保存在 sqlite 中。
稍后,从 sqlite 将其作为单个字符串检索回来,并将其转换回字符串数组。

String ARRAY_DIVIDER = "#a1r2ra5yd2iv1i9der";

public String serialize(String content[]){      
    return TextUtils.join(ARRAY_DIVIDER, content);
}

public String[] derialize(String content){
    return content.split(ARRAY_DIVIDER);
}

Convert it to single String to save it in sqlite.
Later, retrieve it back from sqlite as a single string and convert it back to Array of String.

String ARRAY_DIVIDER = "#a1r2ra5yd2iv1i9der";

public String serialize(String content[]){      
    return TextUtils.join(ARRAY_DIVIDER, content);
}

public String[] derialize(String content){
    return content.split(ARRAY_DIVIDER);
}
跨年 2025-01-05 18:32:44

如果将字符串数组转换为 JSONArray,使用 toString 然后通过首先解析为 JSONArray 来检索它,然后将其转换为字符串数组,会更容易

It would be easier if you convert the array of Strings to JSONArray, use toString and then, retrieve it by parsing first into JSONArray and then, convert it into array of Strings

九歌凝 2025-01-05 18:32:44

看来您的数据库没有按应有的设计。如果要将布尔数据存储在 SQLite 数据库中,可以将布尔值存储为整数 0(假)和 1(真)

It looks like your database is not designed as it should be. If you want to store the boolean data in SQLite database you can store the Boolean values as integers 0 (false) and 1 (true)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文