Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert Array in SQLite Database in android

I want to save weekdays in database, so i thought to store it by assigning int value to each day. i.e

1 -> Selected, 0 -> Not Selected.

Monday = 0/1

Tuesday = 0/1

. . . . .

Sunday = 0/1.

But this will make 7 columns in DB. So I was thinking if anyone can help me with this if I should store it in a single array and retrieve the values for further use. I was reading some examples over internet but didn't get it in a easy way.

like image 201
Harpreet Avatar asked May 21 '12 12:05

Harpreet


2 Answers

To insert 7 values in one column you can use comma separator like this

where Total_Score_P1 is an string array

//string array

String[]  Total_Score =  new String[] { p1e1,p1e2,p1e3,p1e4,p1e5,p1e6 };


// Convderting it into a single string 

String result_ScoreP1 = ("" + Arrays.asList(Total_Score_P1)).
             replaceAll("(^.|.$)", "  ").replace(", ", "  , " );

result_ScoreP1 will be

// output of this

result_ScoreP1 = "p1e1,p1e2,p1e3,p1e4,p1e5,p1e6";

insert it as a single string in database and when retrieve it in again break in parts like

// a string array list

// query fired

public ArrayList<String> rulTable(String id) {
        // TODO Auto-generated method stub
        ArrayList<String> Ruleob = new ArrayList<String>();

        Cursor c_rule;

        try
        {
            c_rule = db.query(NameTable, new String[]{
                    columns1        
            },
            Rule_COurseID + "=" + id ,
                              null, null, 
                              null, null, null);

            c_rule.moveToFirst();

            // if there is data available after the cursor's pointer, add
            // it to the ArrayList that will be returned by the method.
            if (!c_rule.isAfterLast())
            {
                do
                {

                    Ruleob.add(c_rule.getString(0));

                }
                while (c_rule.moveToNext());
            }

            // let java know that you are through with the cursor.
            c_rule.close();
        }
        catch(Exception e)
        {


        }
        return Ruleob;


    }


//list to get elements 

 ArrayList<String>  ListOne = new ArrayList<String>();

ArrayList<String> row ;

    try{
// received values
        row = db.TheTable(id);
        String r1 = row .get(0);

}

catch(Exception e)

{

}


 StringTokenizer st2 = new StringTokenizer(r1, "||");

            while(st2.hasMoreTokens()) {
            String Desc = st2.nextToken();
               System.out.println(Desc+ "\t" ); 
            ListOne.add(Desc); 

//   
            }
like image 180
Avi Dhiman Avatar answered Oct 14 '22 07:10

Avi Dhiman


You can use a binary integer 1= selected 0 =Not Selected (1111111) (0000000)

total seven days so index 0=mon, 1=tues, 2=wed, 3=thurs, 4=friday, 5=sat, 6=sunday..and so on..

here 1111111 means all day selected, 0000000 all day not selected, 0001000 only thursday is selected.

like image 43
Mohammed Azharuddin Shaikh Avatar answered Oct 14 '22 09:10

Mohammed Azharuddin Shaikh