0

I want to take the data from parse.com

public void ParseQueryMap() {
         query = new ParseQuery("MyObject"); 
         query.findInBackground(new FindCallback() {
              public void done(List<ParseObject> myObject, ParseException e) {
              if (e == null) {
                  for ( int i = 0; i < myObject.size(); i++) {              
                              stranaGet = myObject.get(i).getString("Country");
                              oblastGet = myObject.get(i).getString("District");
                              gorodGet = myObject.get(i).getString("City");
                     }
              } 
}

And I want to make all of this data into the database Android

DBHelper dbHelper;
 @Override
     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    
        setContentView(R.layout.main);

            dbHelper = new DBHelper(this);
            ContentValues cv = new ContentValues();
            SQLiteDatabase db = dbHelper.getWritableDatabase();
            String name = etName.getText().toString();
            String email = etEmail.getText().toString();

            cv.put("name", name);
            cv.put("email", email);
            Cursor c = db.query("mytable", null, null, null, null, null, null);

            if (c.moveToFirst()) {


        int idColIndex = c.getColumnIndex("id");
        int nameColIndex = c.getColumnIndex("name");
        int emailColIndex = c.getColumnIndex("email");

        do {
          Log.d(LOG_TAG, "ID = " + c.getInt(idColIndex) + 
                                 ", name = " + c.getString(nameColIndex) + 
                                 ", email = " + c.getString(emailColIndex));
          } while (c.moveToNext());
        } else {
         Log.d(LOG_TAG, "0 rows");
        c.close();
        }

       dbHelper.close();

}

    class DBHelper extends SQLiteOpenHelper {

        public DBHelper(Context context) {
          super(context, "myDB", null, 1);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
           db.execSQL("create table mytable ("
              + "id integer primary key autoincrement," 
              + "name text,"
              + "email text" + ");");
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        }
      }

In this example, the data is taken from EditText, but I want to fill a database with a parse.com. How can this be? UPDATE: I tried to like this

public void ParseQueryMap() {
             query = new ParseQuery("MyObject"); 
             query.findInBackground(new FindCallback() {
                  public void done(List<ParseObject> myObject, ParseException e) {
                  if (e == null) {
                      for ( int i = 0; i < myObject.size(); i++) {              
                                  stranaGet = myObject.get(i).getString("Country");
                                  oblastGet = myObject.get(i).getString("District");
                                  gorodGet = myObject.get(i).getString("City");
                                  cv.put("name",  stranaGet);
                                  cv.put("email", oblastGet);
                                  db.insert("mytable", null, cv);
                         }
                  } 
    }

but nothing happens

3
  • Hi,I wanted to ask if you got this to work, I tried your method as well but it didn't work for me. Commented Feb 2, 2014 at 10:13
  • me too, did you figure this out? also in case there's a complete source on git do put that up. :) Commented Feb 19, 2014 at 13:26
  • You may use an ORM like JDXA to simplify the process of local data storage in SQLite. For example, in the ParseQueryMap method above, instantiate and populate an object (say of class Location with the city, district, and country fields) using the queried data from Parse, and save that object in the local database using the the insert method of JDXA ORM. JDXA facilitates using POJOs in you app and that simplifies your app's development, evolution, and maintenance. Commented Sep 29, 2015 at 22:57

2 Answers 2

2

The query method is used to read data.

To insert data, use the insert method; like this:

db.insert("mytable", null, cv);
Sign up to request clarification or add additional context in comments.

Comments

2

There is a better way to do it.

If you are using some ORM engines for Android (such as OrmLite), which allow you to map database entities to java-classes, integration with parse.com is very simple and straight forward. Actually, when I discovered ORM in android, I cannot live without it any more :)

Let's assume you have your entity-class, mapped to some database table. You can add annotations to its fields for mapping them on to parse.com corresponding class fields. Then you write a code that handles these annotations accordingly.

But actually, you don't have to write the code anymore. Here is a simple implementation of the idea: https://github.com/ntoskrnl/DataSync

Feel free to contribute :)

In short, the library provides methods to convert annotated classes to ParseObject and vice versa. Also it provides you a simple method to synchronize your local and remote data (it uses parse objectId and updatedAt fields to decide what to do)

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.