1

I would like to create a sqlite database starting from some input data contained in a xml file. The xml file contains items like:

<item>rome line1 1 3 4 5 ...</item>

I would like to have a table with three columns, the first two are string columns, the third one an array of integers (or strings). Is it possible to realise this in android studio? How do I put the values to initialize the table?

here is a sample of how I want the table to be:

    public static abstract class FeedEntry implements BaseColumns {
    public static final String TABLE_NAME = "entry";
    public static final String COLUMN_NAME_ENTRY_ID = "entryid";
    public static final String COLUMN_NAME_TITLE = "title";
    public static final String[] COLUMN_NAME_SCHEDULE = "schedule";
}

Here is the function to initialize the table

        String[] myArray = res.getStringArray(R.array.my_array);
    for (String item : myArray) {
        String[] split = item.split("\\s+");

        values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_ENTRY_ID, split[0]);
        values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_TITLE, split[1]);

        for (int i = 2; i < myArray.length - 2; i++) {
            values.put(FeedReaderContract.FeedEntry.COLUMN_NAME_SCHEDULE, Integer.parseInt(split[i]));
        }
        db.insert(FeedReaderContract.FeedEntry.TABLE_NAME, null, values);

    }

here is the array.xml file

<?xml version="1.0" encoding="utf-8"?>

<string-array name="my_array">

    <item>rome line1 1 3 5 7 11 190</item>


</string-array>

However, in what I have now there are some incompatible types, for example I am not able to put an array of integers in values, when I initialize the table.

6
  • you can put your values in CSV format in third column in SQLite. Split it by ',' character when you retrieve your value from third column Commented Oct 6, 2015 at 8:31
  • @Viral could you post me a small example of what you have in mind? Commented Oct 6, 2015 at 8:33
  • For eg, I have an array of integer -> 1 2 3 5 6 7 8, Now I want to insert it into single column, So I'll use CSV format to store values (like 1,2,3,5,6,7,8), And when I retrieve value (which is in CSV), I'll split it by "," character Commented Oct 6, 2015 at 8:36
  • @Viral is there a way to define the CSV format inside xml? Commented Oct 6, 2015 at 8:39
  • I don't know, Though you can convert your array in csv format before adding to xml.This might help you to reduce processing in getting data from xml Commented Oct 6, 2015 at 8:46

2 Answers 2

0

Store schedule as String instead of String[] with ',' separator value :

public static final String COLUMN_NAME_SCHEDULE = "schedule";

String schedule="";
for (int i = 2; i < split.length; i++) {
    if(schedule.trim().length()>0){
        schedule += split[i]+",";
    }else{
        schedule = split[i];
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I understand the idea, but in your code there should be a small mistake, because my app crashes...
I think it is an out of bounds error. the reason is that myArray.length is the length of characters of the string, and not the length of group of characters without the whitespaces. how can I specify the latter one instead of the former one?
0

Contrary to the popular belief the data type is not restricted in sqlite to the creation of the database, at runtime you can insert string where you have declared int. i suggest you declare each column type as blob type refer this link for more sql lite db https://www.sqlite.org/datatype3.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.