Simple Export and Import of a Sqlite Database on Android

import/export to android sqlite database

SQlite database to our local file system-

Function declaration-

        try {
backupDatabase();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}

Function defined-

public static void backupDatabase() throws IOException {
//Open your local db as the input stream
String inFileName = "/data/data/com.myapp.main/databases/MYDB";
File dbFile = new File(inFileName);
FileInputStream fis = new FileInputStream(dbFile);

String outFileName = Environment.getExternalStorageDirectory()+"/MYDB";
//Open the empty db as the output stream
OutputStream output = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer))>0){
output.write(buffer, 0, length);
}
//Close the streams
output.flush();
output.close();
fis.close();
}

simple import/export option of a sqlite database

Try this:

private void importDatabase(String inputFileName) throws IOException
{
InputStream mInput = new FileInputStream(inputFileName);
String outFileName = YOUR_DB_PATH_HERE;
OutputStream mOutput = new FileOutputStream(outFileName);
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer))>0)
{
mOutput.write(mBuffer, 0, mLength);
}
mOutput.flush();
mOutput.close();
mInput.close();
}

How to remove duplicate and sort objects from JSONArray using Java

I wrote this utility a while ago, it sorts a JSONArray of JSONObjects
Only condition is that your JSONobjects must contain the keys you want to sort based on (it also accept a set of keys if you want to sort based on several keys)

import java.util.Collections;
import java.util.Comparator;
import java.util.Random;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class JSONArraySort {
@SuppressWarnings("unchecked")
public static void sortASCE(JSONArray array, Object key) {
Object[] keys = { key };
Collections.sort(array, new JSONArrayComparator(false, keys));
}
@SuppressWarnings("unchecked")
public static void sortDESC(JSONArray array, Object key) {
Object[] keys = { key };
Collections.sort(array, new JSONArrayComparator(true, keys));
}
@SuppressWarnings("unchecked")
public static void sortASCE(JSONArray array, Object[] key) {
Collections.sort(array, new JSONArrayComparator(false, key));
}
@SuppressWarnings("unchecked")
public static void sortDESC(JSONArray array, Object[] key) {
Collections.sort(array, new JSONArrayComparator(true, key));
}
private static class JSONArrayComparator implements Comparator<JSONObject> {
private final Object[] KEYS;
private final boolean DESC;
public JSONArrayComparator(boolean DESC, Object[] KEYS) {
this.KEYS = KEYS;
this.DESC = DESC;
}
@Override
public int compare(JSONObject object1, JSONObject object2) {
int length = KEYS.length;
for(int i = 0 ; i < length ; i++){
String KEY = KEYS[i].toString();
Object one = object1.get(KEY);
Object two = object2.get(KEY);
if(Number.class.isAssignableFrom(one.getClass()) && Number.class.isAssignableFrom(two.getClass())){
Double numOne = Number.class.cast(one).doubleValue();
Double numTwo = Number.class.cast(two).doubleValue();
int compared = 0;
if(DESC){
compared = numTwo.compareTo(numOne);
}else{
compared = numOne.compareTo(numTwo);
}
if(i == KEYS.length - 1 || compared != 0){
return compared;
}
}else{
int compared = 0;
if(DESC){
compared = two.toString().compareTo(one.toString());
}else{
compared = one.toString().compareTo(two.toString());
}
if(i == KEYS.length - 1 || compared != 0){
return compared;
}
}
}
// this shouldn't happen.
return 0;
}
}
//testing...
public static void main(String... args) {
JSONArray array1 = new JSONArray();
for(int i = 0 ; i < 100 ; i++){
Random random = new Random();
int num1 = random.nextInt(10);
int num2 = random.nextInt(10);
JSONObject object = new JSONObject();
object.put("num1", num1);
object.put("num2", num2);
array1.add(object);
}
String[] keys = { "num1", "num2" };
sortASCE(array1, keys);
System.out.println(array1.toString());
}
}

now if you want to remove duplicates you can iterate through them

Set<String> stationCodes=new HashSet<String>();
JSONArray tempArray=new JSONArray();
for(int i=0;i<yourJSONArray.size();i++){
String stationCode=yourJSONArray.getJSONObject(i).getString("stationCode");
if(stationsCodes.contains(stationCode){
continue;
}
else{
stationsCodes.add(stationCode);
tempArray.add(yourJSONArray.getJSONObject(i));
}

}


yourJSONArray= tempArray; //assign temp to original

//here how you can sort it using utility above:
JSONArraySort.sortASCE(yourJSONArray,"distance");

How to remove duplicate objects from json array using hashset?

Below code parses your json and puts it in the HashMap to get unique elements

    HashMap<String,String> hashOut = new HashMap<String,String>();
String input = "{\"others\":[ { \"id\":\"1\", \"caption\":\"test\" }, { \"id\":\"2\", \"caption\":\"self\" }, { \"id\":\"2\", \"caption\":\"self\" }, { \"id\":\"1\", \"caption\":\"test\" }],\"quantity\":[ { \"id\":\"1\", \"caption\":\"self1\" }, { \"id\":\"1\", \"caption\":\"self1\" }]}";
JSONObject jsonObj = new JSONObject(input);
Iterator it = jsonObj.keys();

while (it.hasNext()) {
hashOut.clear();
JSONArray jsInner = (JSONArray) jsonObj.getJSONArray((String) it.next());
for (int i=0;i<jsInner.length();i++) {
JSONObject jo = new JSONObject(jsInner.get(i).toString());
hashOut.put((String)jo.get("id"), (String)jo.get("caption"));
}
System.out.println(hashOut);
}

To get the output as below

{2=self, 1=test}
{1=self1}


Related Topics



Leave a reply



Submit