简体   繁体   中英

SQLiteDatabase Android + parse.com

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

The query method is used to read data.

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

db.insert("mytable", null, cv);

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM