0

I had stored all the numbers in the phone into a arraylist now i need to store all this numbers into sqlitedb, so that i can convert them into excel sheet easily.

List list = new ArrayList();

String number = cur.getString(cur.getColumnIndex(CommonDataKinds.Phone.NUMBER)); list.add(number);

Vishnu M A
  • 239
  • 1
  • 3
  • 18

2 Answers2

1

try this method for store phobne number

public static void storeInDB(ArrayList longs) throws IOException, SQLException {

ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(bout);
for (long l : longs) {
    dout.writeLong(l);
}
dout.close();
byte[] asBytes = bout.toByteArray();

PreparedStatement stmt = null;  // however you get this...
stmt.setBytes(1, asBytes);
stmt.executeUpdate();
stmt.close();

}

public static ArrayList readFromDB() throws IOException, SQLException {

ArrayList<Long> longs = new ArrayList<Long>();
ResultSet rs = null;  // however you get this...
while (rs.next()) {
    byte[] asBytes = rs.getBytes("myLongs");
    ByteArrayInputStream bin = new ByteArrayInputStream(asBytes);
    DataInputStream din = new DataInputStream(bin);
    for (int i = 0; i < asBytes.length/8; i++) {
        longs.add(din.readLong());
    }
    return longs;
}

}

Binesh Kumar
  • 2,763
  • 1
  • 16
  • 23
0

you'd use a ContentValues object to hold the values, and call the insert method of the SQLiteDatabase object. e.g.

  database = SQLiteOpenHelper.getWritableDatabase();
  ContentValues values = new ContentValues();
  for(int i = 0; i < list.size(); i++)
  {
     values.put(CommonDataKinds.Phone.NUMBER, list.get(i);
     database.insert(TABLE_NAME, null, values);
  }

basically what i did was 1) get a reference to the database object as writable; 2) create a contentValues object that will be passed into the insert method of the writable database; 3) iterate over the list, and add each element in the list, to the contentValues object (which is added to database with each iteration). hope this helped some! i'd recommend checking out slidenerd's video on youtube about SQLiteDataBases in android

Bryan Mills
  • 115
  • 9