Insert array into sqlite database in android

I want to keep weekdays in the database, so I decided to save it by assigning an int value every 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 wondering if anyone could help me with this if I have to store it in a single array and get the values ​​for future reference. I read a few examples over the Internet, but didn't get it in a simple way.

+8
android arraylist sqlite
source share
4 answers

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

where Total_Score_P1 is a 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 this

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

insert it as a single row into the database and when you extract it again break up the parts, for example

// list of string arrays

// request started

 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 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); // } 
+10
source share

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

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

here 1111111 means that all day is selected, 0000000 is not selected all day, 0001000 is selected only on Thursday.

+5
source share

I also discovered a way, i.e. I convert your so-called values ​​to a JSON Array , and then save the full JSON string to the entity / field in the database.

This helps maintain value easily and efficiently.

+3
source share

Create another table with a column for each day, a boolean. Create a relationship with this table using integer id (use foreign key). This is a relational way to solve a problem.

+2
source share

All Articles